3

I have found this solution, but perhaps there is a better one, what do you think about it?

class A
{
        #A self-reference
        private $_a;
        #Construcotr makes a self-reference
        public function __construct()
        {
                $this->_a = $this;
        }
        #Some other functions
        public function hello()
        {
                echo 'Hello, world!' . PHP_EOL;
        }
        #Function to destruct just this object, not its parent
        public function myDestruct()
        {
                unset($this->_a);
                var_dump($this);
        }
}

class AProxy
{
        #The proxied object
        public $_a;
        #The constructor has to be rewritten
        public function __construct()
        {
                $this->_a = new A();
        }
        #The destructor
        public function __destruct()
        {
                $this->_a->myDestruct();
                unset($this->_a);
                var_dump($this);
        }
        #Some other functions
        public function __call($fct, $args)
        {
                call_user_func(array($this->_a, $fct), $args);
        }
}

echo 'START' . PHP_EOL;
#Use class AProxy instead of class A
$a = new AProxy();

$a->hello();

unset($a);

#Otherwize you need to trigger the garbage collector yourself
echo 'COLLECT' . PHP_EOL;
gc_collect_cycles();

echo 'END' . PHP_EOL;

If I use the class A as-is, then unsetting doesn't work because A has a self-reference in one of its property.

In this case I need to manually call the garbage collector.

The solution I have found is to use a proxy class, called AProxy, which calls a special function named myDestructor in A which only destruct the A class and not its parent.

Then the destructor of AProxy calls myDestructor on the instance of A.

In order to make AProxy resemble the A class, I reimplemented the __call function (the setters and getters for properties may also be overloaded).

Do you have a better solution than that?

  • 2
    I think the better question here is - why does your class have a self reference? Not trying to be rude, but I can't think of a legitimate case for this. – Colin M Feb 06 '13 at 15:45
  • Why would you create a self containing object? Is this a hypothetical scenario or is this a real usage? – John McKnight Feb 06 '13 at 15:47
  • 1
    Upgrade to 5.3. This kind of circular reference is captured for you when the garbage collector is invoked (typically at scope change, so when you leave a function, or at certain other times). No, it won't get called immediately, and there's no way to without manually invoking the GC or just not doing it in the first place... – ircmaxell Feb 06 '13 at 16:06
  • I have a self reference because the A class in my case is a Database class that already inherits from PDO class, and it also keeps a cache of prepared requests, which contain references to the PDO object. I solved the problem having a property $_pdo instead of inheritance in my Database class, overloading the __call() function, and unsetting all references of the PDO object in the desctructor. – user2047060 Feb 08 '13 at 08:54
  • 1
    The problem arise in many legitimal cases. For example in a tree structure, where each node holds a reference to its parent node. – user2047060 Feb 08 '13 at 09:19

0 Answers0