Prev: Netbeans XDebug Breakpoints Socket Accept
Next: Recupero di pen drive e memory card non più accessibili
From: Daniel Kolbo on 11 Jul 2010 18:19 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 `
From: Richard Quadling on 12 Jul 2010 07:00 On 11 July 2010 23:19, Daniel Kolbo <kolb0057(a)umn.edu> 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.
From: Daniel Kolbo on 12 Jul 2010 17:54 Richard Quadling wrote: > On 11 July 2010 23:19, Daniel Kolbo <kolb0057(a)umn.edu> 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 `
From: Richard Quadling on 13 Jul 2010 04:46 On 12 July 2010 22:54, Daniel Kolbo <kolb0057(a)umn.edu> wrote: > Richard Quadling wrote: >> On 11 July 2010 23:19, Daniel Kolbo <kolb0057(a)umn.edu> 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 > ` > > What version of PHP are you using?
From: Richard Quadling on 13 Jul 2010 04:59 On 13 July 2010 09:46, Richard Quadling <rquadling(a)gmail.com> wrote: > On 12 July 2010 22:54, Daniel Kolbo <kolb0057(a)umn.edu> wrote: >> Richard Quadling wrote: >>> On 11 July 2010 23:19, Daniel Kolbo <kolb0057(a)umn.edu> 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 Aha! $c = new C; At this stage $c->c will be a reference to the static A::$a = 3. $b = new B; Now, as B's constructor calls A's constructor which replaces the static A::$a with a reference to the instance $b, the static A::$a should now be $b $cee = new C; At this stage $cee->c will be a reference to the static A::$a = $b. But, when var_dump()'d, $c->c !== $cee->c, and I think they should as both have been assigned to a reference of a static. It would seem to be a bug. I get the same output for V5.0.0 to V5.3.3RC2
|
Next
|
Last
Pages: 1 2 Prev: Netbeans XDebug Breakpoints Socket Accept Next: Recupero di pen drive e memory card non più accessibili |