static call to an instance method in a class definition

--0016367b5dbc3fefef048bfc2682
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hello,
Making a mistake I just come to discover ( for myself ) a strange fiture in
php,
lets see an example :

class A{

public $foo =3D 'bar';

public function write(){

print($this->foo);

}

}


class B{

public $foo =3D 'gnagnagna';



public function write(){

A::write();

}

}


$var =3D new B;

$var->write();


This code will not throw excaption and output gnagnagna, it's kind of cross
d=C3=A9finition something,
I know something like that exists in other language but I didn't expect PHP
to work like that.

Do you know this ? And why ?

--0016367b5dbc3fefef048bfc2682--
Samuel [ Do, 22 Juli 2010 18:09 ] [ ID #2044960 ]

Re: static call to an instance method in a class definition

Hi.

I knew about this "feature" ($this is used from the caller scope if
the called method is static), but I didn't checked the manual about
this.
Now I did it.

http://php.net/manual/en/language.oop5.static.php
"Because static methods are callable without an instance of the object
created, the pseudo-variable $this is not available inside the method
declared as static."

some comments point out this behaviour, but either the code, or the
documentation is wrong.

Tyrael

On Thu, Jul 22, 2010 at 6:09 PM, samuel <zouliloua [at] gmail.com> wrote:
> Hello,
> Making a mistake I just come to discover ( for myself ) a strange fiture =
in
> php,
> lets see an example :
>
> class A{
>
> =C2=A0public $foo =3D 'bar';
>
> =C2=A0public function write(){
>
> =C2=A0 print($this->foo);
>
> =C2=A0}
>
> }
>
>
> class B{
>
> =C2=A0public $foo =3D 'gnagnagna';
>
>
>
> =C2=A0public function write(){
>
> =C2=A0A::write();
>
> =C2=A0}
>
> }
>
>
> $var =3D new B;
>
> $var->write();
>
>
> This code will not throw excaption and output gnagnagna, it's kind of cro=
ss
> d=C3=A9finition something,
> I know something like that exists in other language but I didn't expect P=
HP
> to work like that.
>
> Do you know this ? And why ?
>

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ferenc Kovacs [ Do, 22 Juli 2010 18:20 ] [ ID #2044961 ]
PHP » gmane.comp.php.windows » static call to an instance method in a class definition

Vorheriges Thema: RE: [phpXperts] No Warping in results [1 Attachment]
Nächstes Thema: How long before V5.3.3 hits the shelves?