Re: PHP5 Class problem

From: Date: Fri, 28 Jan 2005 18:51:42 +0000
Subject: Re: PHP5 Class problem
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-207621@lists.php.net to get a copy of this message
Richard Lynch wrote:
daniel@electroteque.org 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();

« previous php.general (#207621) next »