Re: Static Class Member References

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

 



Richard Quadling wrote:
> On 11 July 2010 23:19, Daniel Kolbo <kolb0057@xxxxxxx> wrote:
>> Hello PHPers,
>>
>> I'm having some trouble understanding some PHP behaviour.  The following
>> example script exhibits the behaviour which I cannot understand.
>> [code]
>> <?php
>>
>> class A
>> {
>>        public static $a = 3;
>>
>>        function __construct()
>>        {
>>                //self::$a = $this; //[i]
>>                self::$a =& $this; //[ii]
>>        }
>> }
>>
>> class B extends  A
>> {
>>        function __construct()
>>        {
>>                parent::__construct();
>>        }
>> }
>>
>> class C {
>>        var $c;
>>
>>        function __construct()
>>        {
>>                $this->c =& A::$a;
>>        }
>>
>> }
>>
>>
>> $c = new C;
>> $b = new B;
>> $cee = new C;
>>
>> var_dump($c->c); // [i] prints object(B), but [ii] prints int 3
>> var_dump($cee->c); // [i] prints object(B), and [ii] prints object(B)
>>
>> ?>
>> [/code]
>>
>> Why does $c->c print 'int 3' ?
>>
>> I'm nervous to use "self::$a = $this;" because I don't want to be
>> copying the whole object.  However, isn't $this just a reference to the
>> object, so "self::$a = $this;" is just copying the reference and not the
>> actual object, right?
>>
>> Thanks in advance
> 
> 
> What do you think the value should be?
> 
> A static property is bound to the class and not to an instance of the class.
> 
> So, &A::$a is a reference to the static value. If you alter the value,
> it will be altered for a subclasses of A and for any other reference
> to it.
> 

I think
var_dump($c->c); would print object(B), but it's printing int 3.

The reference is *not* being updated.  I think this is a bug.  What do
you think?

Thanks
`


-- 
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