possible bug with static variables?

From: Date: Sat, 01 Jan 2005 05:22:46 +0000
Subject: possible bug with static variables?
Groups: php.internals 
Request: Send a blank email to internals+get-14255@lists.php.net to get a copy of this message
Hi, I have discovered some odd behavior with static variables, and wonder if it is a bug. In both PHP4 and PHP5, the code below prints: not registered, class pear_common not registered, class archive_tar I.e., because the PEAR function is called with $this->PEAR(), the static variable is reset as if the PEAR() function were re-defined! Is this expected behavior? I was under the impression that static declared a per-class static variable. <?php class PEAR {
    function PEAR($error_class = null)
    {
        static $registered = false;
        $classname = get_class($this);
        while ($classname && strcasecmp($classname, "pear")) {
            $destructor = "_$classname";
            if (method_exists($this, $destructor)) {
                if (!$registered) {
                    echo 'not registered, class ' . get_class($this) . "\n";
                    $registered = true;
                }
                break;
            }
        }
    }
} class PEAR_Common extends PEAR {
    function PEAR_Common()
    {
        $this->PEAR();
    }
    function _PEAR_Common()
    {
    }
} class Archive_Tar extends PEAR {
    function Archive_Tar()
    {
        $this->PEAR();
    }
    function _Archive_Tar()
    {
    }
} $a = &new PEAR; $b = &new PEAR_Common; $c = &new Archive_Tar; ?>

« previous php.internals (#14255) next »