Re: References challenge with PHP4

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2007-03-20 at 11:52 +0100, Jochem Maas wrote:
> ok, I tried it in a whole number of variations -
> no joy.
> 
> you should use php5 if you want this kind of reference
> stuff - in php5 it just works, php 4 will give you big headaches.

Now now, don't be calling PHP5 "ALL THAT" when it can't do it right
either (or did you not check it using PHP5 ;) ;) Everything works fine
in PHP4 (also for PHP5 in this case if you just understand that static
vars are buggy *lol* -- and this is first time I've noticed this bug).
Solution follows:

<?php

class B
{
    function B( &$a )
    {
        $this->a = &$a;
    }

    function run()
    {
        echo $this->a->msg."\n";
    }
}

class A
{
    var $msg = null;

    function A()
    {
        $this->b = &new B( $this );
    }

    function &getInstance()
    {
        static $inst = array();

        if( !isset( $inst['staticIsBuggy'] ) )
        {
            $inst['staticIsBuggy'] = &new A();
        }

        return $inst['staticIsBuggy'];
    }

    function run()
    {
        $this->b->run();
    }
}

$a = &A::getInstance();
$a->msg = "Hello world !";

// Some tasks, flow control, scope changes, ...

$a = &A::getInstance();
print "{$a->msg}\n"; // This should print "Hello world !"
$a->run(); // This should print "Hello world !"

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux