...

View Full Version : Class in class.



marek_mar
09-24-2004, 08:53 PM
I'm experimenting a little with classes again and I would like to know how to call a method from a sibling (another child the parent class) class (if it is posible).
Why I was experimenting I found out that:


<?
class foo
{
public $value = true;

function greet()
{
print 'Hello!';
}
}
class bar extends foo
{
public $value = true;

function __construct()
{
parent::greet();
}
}
?>
classes (foo and bar) are parent and child but these:


<?
class foo
{
public $bar = '';

function __construct()
{
$this->bar = new bar();
}

}
class bar
{
public $value = true;
function __construct()
{
parent::greet();
}
}
?>
are not (foo is not recognised as bar's parent).
I can access any method from any place above bar (eg. $foo->bar->method or $this->bar->method) but I can't do access anything from bar. Is it possible? How? I'm sure I'm msissng something I shouldn't.

firepages
09-25-2004, 03:55 AM
In your second class there is no parent->child relationship , that only happens when you enforce such with extends() , since you are using PHP5 you can also use interfaces (implements) to show a different type of relationship.

Your second example is an example of composition/aggregation there are aggregate functions in PHP4 but I have never used them.

Object composition is a common method of working with groups of classes without inheritance and with more flexibility than inheritance , you can only have 1 parent class in PHP , ok that parent can itself extend another class but it gets difficult to manage complex code in a single heirachy and therefore limits complexity.

extended classes generally extend another clase with related/similar functionality , composite/aggreate classes and methods need not be related at all to the calling object.

e.g.
class human{}
class male extends human{}
class female extends human{}

so far thats workable, but what about

class girlfriend{}

OK girlfriend is indeed human , but in relation to our instance of human its not relevant. Multiple inheritance would pull that together but 1) PHP don't have multiple inheritance & 2) It never will have :D

so girlfriend is a good class to hold in an instance of our man or woman
class man extends human{
function girlfriend( $array_stats ){
$this->girlfirend = new girlfriend( $array_stats ) ;
}
}

now how you access the methods and properties of girlfriend is up to you though there is no 1 correct way to to it (in code or real life ;))



<?
class foo{
function __construct(){
$this->bar = new bar( $this );
$this->bar->reply();
}

function greet(){
echo 'hello';
}

}
class bar{
function __construct( $calling_object ){
$calling_object->greet();
/*or indeed*/
foo::greet();
}
function reply(){
echo 'hello back';
}
}
?>

marek_mar
09-25-2004, 10:37 AM
Thank you! I did not want multiple inheritance, I can live without it. Though the $calling_object restores a 'broken link' from the from the inner class to the outer class. I only wanted to access the methods from the other subclass from the first one. Some code to clarify what I was talking about):


<?
class main
{
function __construct()
{ // This creates the links to sub-classes of this class
$this->target = new target($this);
$this->bar = new bar($this);
/*
$this needs to be sent becouse the subclass would not
be able to use the methods and propertys of the main class.
PHP doesn't do anything to allow this, so you must do it yourself.
*/
}

}

class bar
{ // The first sub-class.
public $parent;
public $other_class;
function __construct($object)
{ // This makes a connection to the main class.
$this->parent = $object;

}
function reply(){
echo 'hello back' . "\n";
// Now we can call a method of the main class or any other sub-class the main class has.
$this->parent->target->reply();
// or we can link the classes together if we need to use it more than once.
$this->other_class = $this->parent->target;
$this->other_class->reply();
}
}

class target
{ // The second sub-class.
function reply()
{
echo 'hello from target' . "\n";
}
}

$test = new main();
$test->bar->reply();
/*
Use: If you want PHP code to be structured in a hierarchical way
(you call all methods in any subclass like $main_class->sub_class->method)
way and want to be bale to get data from another branch of classes.
*/
?>

The only question to end my test is: Is this a good Idea to hierachicly set classes? Is it efficient?

firepages
09-26-2004, 04:53 AM
The only question to end my test is: Is this a good Idea to hierachicly set classes?

Well the point really is that you are not setting a heirarchy as such with composition/aggregation of other classes.

A true parent-child relationship requires that the parent class extends a child class and that is not the case in your code above.

My only complaint about the code above would be the usage of `$parent` & herarchical terms for your 'sub-classes' as they are not subclasses & there is no heirarchy , they are composite/external objects , semantics I know , but one day they come back to bite you ;)

The danger of working like this is that you can end up in class $x calling a method of class $b that actually gets passed via class $g & $r along the way , it should be calling class $b directly , not only from an efficiency standpoint but also in readability.

e.g.

$this->parent->target->reply();

is about as far away as you want to get in terms of abstraction , any further than that and you head will hurt !

marek_mar
09-26-2004, 01:51 PM
Thank you for the reply. One more thing: What is the realtion of the classes in the main class to the main class? You said they are 'stand alone' but you can't say they have no relation at all. How is that relation called?
I now have some sort of proof that there is some kind of hierarchy relationship (print_r($GLOBALS['test']) from that code).

firepages
09-27-2004, 03:12 PM
The objects instansiated within your classes are composite objects , I don't know any other way to describe them.

More information can be gleaned at http://www.php.net/manual/en/ref.objaggregation.php

but with your current composite objects there is no child-parent relationship even though the composite objects 'belong' to the main class.

marek_mar
09-27-2004, 04:11 PM
Well from the manual it looks like that it is only possible in PHP but they don't really know how they want to be in the end yet.
So there is no parent-child relationship but they are connected in another way "aggregated" right?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum