Casting object to another class
Posted: Sat Mar 28, 2009 10:06 am
Hey there, this is more theory and design than a question because i think there are no perfect solution, so i'll explain what i'm trying to do and you tell me what you think...
I have a library used to manage data using drivers and objects called:
datatable
datacolumn
datarow
idataadapter
mysqldataadapter
arraydataadapter
Now i have a class called gridview which is used to create a full featured grid component in html. All of this is in dev right now but here is where i bug.
When i receive the data in the gridview control, i wish to be able to extend the datacolumn class to create gridviewcolumn class. This class extends the datacolumn because i wish to keep the datacolumn rules and functionnalities close to me. The newer class adds display properties relative to the column. When i loop the $dataset->columns() array, i receive all the datacolumns objects, i wish i could simply cast them to gridviewcolumn which extends the already existing datacolumn...
PHP doesn't support casting objects to other object types even in a scenario where the target type would be an extension of the current type.
So we have 3 scenarios:
Requirements:
- Keep the objects in one copy only, for example, i dont' want to clone objects and create a new copy of it, the goal is to limit processing and memory usage while still allowing the best access/extension methods
Scenario 1
is impossible, lets forget it, although it would have been the best solution, maybe i could submit a patch for php5 and php6
Scenario 2,3,4
We could still extend the datacolumn but is a bit useless because the constructor would require the instance to be rebuilt and what we wish to achieve is keep the same reference all around the application without cloning the objects, so the new gridviewcolumn class should not extend the datacolumn class.
Scenario 2
We could implement stub functions to call the datacolumn's properties and functions of the datacolumn object that we keep a reference to inside the class. Problem with this is that if the datacolumn changes we could need to change the gridviewcolumnn to mimic the changes in the stub functions.
Scenario 3
Simply create a new class that copies the information and mimics the effect of the column class. In my opinion, doing that defeats the objective of OO programming where encapsulation and reuse plays a primary role. Too much work if there is a bug in the column class and prone to errors when trying to mimic the effect of the column class.
Scenario 4
Create a gridviewcolumn class that has the datacolumn property and returns it if the user wants to use it. The gridview can use it too easily because it is aware of the connection between the two. The only drawback i see is that to access the datacolumn object you need an additionnal step in your code, but scenario 2 does the same thing but hides it...
Ok, so what do you think? I'm really looking at S4 as my solution but i'd like your input on the subject...
I have a library used to manage data using drivers and objects called:
datatable
datacolumn
datarow
idataadapter
mysqldataadapter
arraydataadapter
Now i have a class called gridview which is used to create a full featured grid component in html. All of this is in dev right now but here is where i bug.
When i receive the data in the gridview control, i wish to be able to extend the datacolumn class to create gridviewcolumn class. This class extends the datacolumn because i wish to keep the datacolumn rules and functionnalities close to me. The newer class adds display properties relative to the column. When i loop the $dataset->columns() array, i receive all the datacolumns objects, i wish i could simply cast them to gridviewcolumn which extends the already existing datacolumn...
PHP doesn't support casting objects to other object types even in a scenario where the target type would be an extension of the current type.
So we have 3 scenarios:
Code: Select all
$mycolumn = (gridviewcolumn)$datacolumn; //Doesn't work because PHP doesn't support it at all, what a shame
$mycolumn = new gridviewcolumn($datacolumn); //Version 1 implements stub functions that call a reference to the datacolumn
$mycolumn = new gridviewcolumn($datacolumn); //Version 2 reimplements and copies the info from the column and discards it (Not effective)
$mycolumn = new gridviewcolumn($datacolumn); //Version 3 implements gridview specific properties and keep a copy of the data column somewhere and the code must ask for the datacolumn through a property accessor (probably the best way)- Keep the objects in one copy only, for example, i dont' want to clone objects and create a new copy of it, the goal is to limit processing and memory usage while still allowing the best access/extension methods
Scenario 1
is impossible, lets forget it, although it would have been the best solution, maybe i could submit a patch for php5 and php6
Scenario 2,3,4
We could still extend the datacolumn but is a bit useless because the constructor would require the instance to be rebuilt and what we wish to achieve is keep the same reference all around the application without cloning the objects, so the new gridviewcolumn class should not extend the datacolumn class.
Scenario 2
We could implement stub functions to call the datacolumn's properties and functions of the datacolumn object that we keep a reference to inside the class. Problem with this is that if the datacolumn changes we could need to change the gridviewcolumnn to mimic the changes in the stub functions.
Scenario 3
Simply create a new class that copies the information and mimics the effect of the column class. In my opinion, doing that defeats the objective of OO programming where encapsulation and reuse plays a primary role. Too much work if there is a bug in the column class and prone to errors when trying to mimic the effect of the column class.
Scenario 4
Create a gridviewcolumn class that has the datacolumn property and returns it if the user wants to use it. The gridview can use it too easily because it is aware of the connection between the two. The only drawback i see is that to access the datacolumn object you need an additionnal step in your code, but scenario 2 does the same thing but hides it...
Ok, so what do you think? I'm really looking at S4 as my solution but i'd like your input on the subject...