Re: PHP5 Class problem

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

 



Richard Lynch wrote:
daniel@xxxxxxxxxxxxxxxx wrote:

...


how odd, i have assumed having a class static you could still throw around variables inside it, or its only meant to stay in the one static method so executing it like


As I understand it...

It's not that you can't use any variables at all -- It's that "$this"
doesn't make any sense in that context.

"$this" refers to the particular instance of the object you have created
with "new XYZ"

$xyz = new XYZ();
$xyz->bar();
When bar refers to "$this" it means the same as the "$xyz" instance.

If you are calling a static method XYZ::foo() then, really, there *IS* no
object inside of foo() to be called "$this" -- You didn't create an
instance of XYZ, so there is no instance to be named "$this" -- There is
only the class definition and the method.

It's an object that's not there -- more like the idea of an object without
having an actual concrete object to hang on to.

Hope that helps make sense of it all.

Hell, hope that's actually correct! :-^


its correct. only thing it gets a little more complicated, personally I kind of got lost in all the internals discussion surround this behaviour... I just code so that I don't use or accidentally trigger it... in some cases this means using a protected/private method for an object. It easiest to see what I'm talking about if you run some code (tested php5):


class XYZ { function doit() { var_dump( isset($this)); } }

class ABC {
function doit()
{ var_dump( isset($this)); }
function doMore()
{ XYZ::doit(); $this->doit(); ABC::doit(); }
}

XYZ::doit();echo"--\n";
ABC::doit();echo"--\n";
$x = new XYZ;
$a = new ABC;
$x->doit();echo"--\n";
$a->doit();echo"--\n";
$a->doMore();

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