PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
class Test
{
private $testing;
public function __construct ()
{
}
public function setTesting ($testing)
{
$this->testing = $testing;
}
}
class Testing
{
private $test;
public function __construct ()
{
$this->test = new Test();
}
public function setTest ($test)
{
$this->test = $test;
}
}
$test = new Test();
$testing = new Testing();
$test->setTesting($testing);
$testing->setTest($test);
?>
The problem is, the original concept promotes really nasty coupling. If they are that integrated, you may want to consider building them together or rethinking their interactions more carefully.
Jenk wrote:Globals within a class/object are usually a nono. what happens if $test is not defined in the global scope?
feyd's example is the best method - use a setter.
Some people use globals to hack a way of creating a singleton. It's better to use static properties, but some would argue that (for the sake of testing) singletons should be avoided at all.