Page 1 of 1

inheritance problems

Posted: Mon Jun 26, 2006 5:25 am
by mcccy005
This is a pretty complicated problem I have (to explain that is) with probably too much code to put here.
What I'm doing is creating classes to display a set of results from a database.

I have (from bottom to top)
1. result_cell (which is a value of an individual cell within a table)
2. result_field (which is a row of a table - ie. an array of result_cells)
3. result_set (which is an array of result_fields)

1 extends 2 and 2 extends 3.

Here is an implementation of the objects:

Code: Select all

$cell1 = new result_cell("First Name");
$cell2=new result_cell("Last Name");
$cell3=new result_cell("Age");

$field=new result_field(array($cell1, $cell2, $cell3), true); //true represents $is_title (see below)

$search_results=new result_set(array($field1));
If I parse any optional variables to a $result_field object (eg. one optional variable was $is_title) to represent if the row of the table (result_field) was the Title of the table or not; each cell object (result_cell) such as $cell1 is not actually a child of the result_field object (eg. $fields). I CAN create a new cell, and for each cell say that it $is_title=true; but I don't want to have to do this for every cell, when it can be implemented for an entire row.
Any suggestions????

Secondly; in the display( ) method of each result_cell object, if $is_title is true, and the no URL has been specified for that particular cell, then the entire result_set will be sorted/re-ordered based on the data in that particular column. (Using another $data_type variable, I will call either a date_sort; int_sort; alpha_sort etc etc etc.), but each row is to contain the same data.
However; for this to work, I need to somehow be able to see ALL of the data in the grandparent (the result_set object); (which I can't do at the moment as I can't even see values in the parent of the cell object ).

Thanks in advance.

Posted: Mon Jun 26, 2006 6:22 am
by Chris Corbyn
You might want to look into creating a cyclic object structure (recursive) between each object. It looks bizarre at first but comes in very handy.

Code: Select all

class parentClass
{
    var $obj;
    
    function parentClass(&$obj)
    {
        $this->obj =& $obj;
        $this->obj->loadParent($this);
    }
}

class childClass
{
    var parentObj;
    
    function loadParent(&$obj)
    {
        $this->parentObj =& $obj;
    }
}

$myObject =& new parentClass(new childClass);
Now parentClass can see childClass through $this->obj; And childClass can see parentClass through $this->parentObj. The recursion comes in the fact that if childClass reads $this->parentObj->obj it can see itself again. It's a good method to acheive this sort of design.

Posted: Mon Jun 26, 2006 6:45 am
by mcccy005
Thanks for that; but um....I have NO idea what that's about sorry!!

I'm extremely new to this whole php; parsing variables by reference etc.


I've gone over and over your code but I really have no idea what it's doing!!
Is $obj supposed to represent the entire class??
Also, why would I use '&' when I create a new object like on teh bottom line of your code (or is that just your preferred method??)

I'll keep re-reading it but if you could briefly walk me through, that would be great!
Thanks again.

Posted: Mon Jun 26, 2006 6:59 am
by Chris Corbyn
mcccy005 wrote:Thanks for that; but um....I have NO idea what that's about sorry!!

I'm extremely new to this whole php; parsing variables by reference etc.


I've gone over and over your code but I really have no idea what it's doing!!
Is $obj supposed to represent the entire class??
Also, why would I use '&' when I create a new object like on teh bottom line of your code (or is that just your preferred method??)

I'll keep re-reading it but if you could briefly walk me through, that would be great!
Thanks again.
The title of your thread says inheritance but what you demonstrated in your first post was not inheritance, it was lazy loading of objects. Basically inheritance means that you're creating a class which actually extends another class using the extends keyword. That measn that when you instantiate (use the new keyword) your class it inherits (own/has access to) the properties and objects of the class it extends as if they belong to itself.

Loading objects is another matter because rather than a class inheriting from another class to create a single object (variable) you actually are creating two classes and passing one into the other. The object which you pass this into may be referred to as a "parent" like with inheritance and the object which is being passed is the child. In some systems these may be referred to as "pluggable" and "plugin" or "observable" and "observer".

In my example, we create an instance of the class "parentClass". At the same time we create an instance of the class "childClass" in the constructor and parentClass then stores this created object as a property. parentClass then runs the method "loadParent()" in childClass and passes an instance of itself ($this) to the child.

It may be easier to see if we do it using those steps:

Code: Select all

class parentClass
{
    var $obj;
   
    function parentClass(&$obj)
    {
        $this->obj =& $obj;
        $this->obj->loadParent($this);
    }
}

class childClass
{
    var $parentObj;
   
    function loadParent(&$obj)
    {
        $this->parentObj =& $obj;
    }
}

$childObject =& new childClass;
$myObject =& new parentClass($childObject);
The & symbols are just reference operators. They can be forgotten about in PHP5 but in PHP4 they are needed if you want PHP to behave like other languages when passing objects around. Basically, without them PHP will create a copy of the object in exactly the same state as it was in but it will not be the same object.

This can be seen by doing something simple like this (I'll use the code above again so you maybe pick up something more):

Code: Select all

class parentClass
{
    var $obj;
   
    function parentClass(&$obj)
    {
        $this->obj =& $obj;
        $this->obj->loadParent($this);
    }
}

class childClass
{
    var $parentObj;
    var $number = 10;
    
    function loadParent(&$obj)
    {
        $this->parentObj =& $obj;
    }
    
    function setNumber($num)
    {
        $this->number = $num;
    }
    
    function getNumber()
    {
        return $this->number;
    }
}

$childObject =& new childClass;

echo $childObject->getNumber(); //10
$myObject =& new parentClass($childObject);

$myObject->obj->setNumber(42);

echo $childObject->getNumber(); //42 because the parent is looking at the exact same object

////
//// Now if we did it without the references
////

class parentClass
{
    var $obj;
   
    function parentClass($obj)
    {
        $this->obj = $obj;
        $this->obj->loadParent($this);
    }
}

class childClass
{
    var $parentObj;
    var $number = 10;
    
    function loadParent($obj)
    {
        $this->parentObj = $obj;
    }
    
    function setNumber($num)
    {
        $this->number = $num;
    }
    
    function getNumber()
    {
        return $this->number;
    }
}

$childObject = new childClass;

echo $childObject->getNumber(); //10
$myObject = new parentClass($childObject);

$myObject->obj->setNumber(42);

echo $childObject->getNumber(); //10 because the parent only set the number in the copy, not the same object
echo $myObject->obj->getNumber(); //42 because we're looking at the copy here

Posted: Mon Jun 26, 2006 7:22 am
by mcccy005
Alright - im starting to get it. Thanks again.
(I didn't realise 'function parentClass( )' in parentClass was the constructor.....I thought PHP only allowed __construct( ) so that makes things a little bit clearer for me)

One (hopefully final) question....
With respect to:

Code: Select all

$childObject =& new childClass; 
$myObject =& new parentClass($childObject);
I still don't quite understand the & being used here? And with PHP5, don't I still need to use the ampersand for parsing by reference??

I appreciate you going through this for me too and if you're aware of any decent reading material that will help me (didn't find anything on a quick google search) feel free to let me know where to find it.

Posted: Mon Jun 26, 2006 7:27 am
by Chris Corbyn
mcccy005 wrote:Alright - im starting to get it. Thanks again.
(I didn't realise 'function parentClass( )' in parentClass was the constructor.....I thought PHP only allowed __construct( ) so that makes things a little bit clearer for me)

One (hopefully final) question....
With respect to:

Code: Select all

$childObject =& new childClass; 
$myObject =& new parentClass($childObject);
I still don't quite understand the & being used here? And with PHP5, don't I still need to use the ampersand for parsing by reference??

I appreciate you going through this for me too and if you're aware of any decent reading material that will help me (didn't find anything on a quick google search) feel free to let me know where to find it.
Ah sorry I just assumed you'd be using PHP4. Use __construct() in PHP5 yes. It's backward compatible though so the name of the class still works as a constructor ;)

The ampersands don't matter in PHP5 neither. References are used by default in PHP5 anyway. The only time you really need them is if your code is to be used in PHP4 too or if it's not an object you're passing).

I'm not sure about reading material for what I posted... I was looking for some myself at the time I devised the system but I couldn't find a lot for PHP based apps. The observer pattern uses a similar way of loading in objects so you may want to search for that although if you're new to OOP I'd maybe lay-off patterns until you get the hang of it.

Posted: Mon Jun 26, 2006 7:43 am
by mcccy005
Thanks so much.

Not new to OOP - just learned Java but havent touched programming since 2003 so all the inheritance stuff etc. is slowly coming back and fortunately I remember something about patterns from Uni, so i'll be sure to read some stuff up on that 'observer' pattern and maybe a few others.

Sorry to ask this AGAIN, but with the ampersand, I still need to use it with loadParent(&obj) but just not when I create the new object?? Otherwise, like you said with your second example, it won't output the right number.
Am I correct on that?? (Maybe I should do some reading on that too)
Promise I won't bug you again after this!!

Posted: Mon Jun 26, 2006 8:06 am
by Chris Corbyn
mcccy005 wrote:Thanks so much.

Not new to OOP - just learned Java but havent touched programming since 2003 so all the inheritance stuff etc. is slowly coming back and fortunately I remember something about patterns from Uni, so i'll be sure to read some stuff up on that 'observer' pattern and maybe a few others.

Sorry to ask this AGAIN, but with the ampersand, I still need to use it with loadParent(&obj) but just not when I create the new object?? Otherwise, like you said with your second example, it won't output the right number.
Am I correct on that?? (Maybe I should do some reading on that too)
Promise I won't bug you again after this!!
With PHP5 you don't even need the ampersand in loadParent() ;) PHP handles it for you.

Posted: Mon Jun 26, 2006 8:15 am
by bdlang