I've built a number of data-driven PHP sites. But with the advent of PHP5, I'm migrating much of my code to be completely object-oriented and I really like the approach.
I'm stumbling on one key thing. It seems to me that the PHP language is missing a solution for referencing the object that CONTAINS the current object (not extends).
So here's my example ...
<?php
class myFoot
{
private $has_warts = "many!";
private $toe;
public function __construct()
{
echo "CREATING FOOT<br />" . "\n";
$this->toe = new myToe();
}
public function Warts()
{
return $this->has_warts;
}
}
class myToe
{
public function __construct()
{
echo "CREATING TOE<br />" . "\n";
echo $GLOBALS['foot']->Warts(); /* Doesn't work! */
}
}
$GLOBALS['foot'] = new myFoot();
echo "FOOT IS " . (( is_object( $GLOBALS['foot'] )) ? '' : 'NOT ' ) . ' AN OBJECT!';
?>
[INDENT]
RESULT ...
CREATING FOOT
CREATING TOE
Fatal error: Call to a member function Warts() on a non-object in /Library/WebServer/Documents/warts.php on line 25
[/INDENT]
Here's where I'm stuck: How to I reference the myFoot object from myToe to find out if it has warts? parent:: doesn't work because myToe doesn't EXTEND myFoot -- which is exactly what I want in this case. It's not appropriate for myToe to extend myFoot because while myFoot might have warts, that doesn't mean that myToe does.
HOW DO I POINT TO myFoot from myToe ???
Where am I going wrong? I've tried using the $GLOBALS approach, but it seams that $GLOBALS isn't always in scope. Further it seems weird to use $GLOBALS to reference the object that contains the object you're calling $GLOBALS from.
Why isn't there a way to access the "parent" object -- parent in the sense that it's the object that contains the object -- not extends!