static call to an instance method in a class definition

static call to an instance method in a class definition

am 22.07.2010 18:09:36 von Samuel

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

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

am 22.07.2010 18:20:02 von Ferenc Kovacs

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 wrote:
> 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 cro=
ss
> définition 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