$this availability inside static-functions

php.internals

Sebastian Mendel

21 years ago
Hi, at first, im really really sorry to bother the internals-list with this problem (especially Derick for coming back from playing somewhere else ... (<Pine.LNX.4.62.0506291622130.14983@localhost>) ), but i didnt find an answer on php.general or german php mailinglist if anybody just can point me to any discussions, websites, bugs or anything else related to the problem below. or just what is the point behind this behaviour? *PHP 4* how can i check if a method is called statically when called from inside another object? (without debug_bactrace()) class foo { function bar() { if ( isset( $this ) ) { return 'not static'; } return 'static'; } function bar2() { foo::bar(); } } // returns 'static' echo foo::bar(); // returns 'not static' but should be 'static' $foo = new foo; echo $foo->bar2(); *PHP 5* and btw. does anyone know why it is not good to let one function handle both methods of calling (static and not static) as in PHP 5 you will get notices if called wrong - but PHP doesnt support multiple function-definitions like: class foo { static function bar() { return ANY_DEFAULT_VALUE; } function bar() { return $this->value; } } or class foo { static function bar( $param ) { return $param; } function bar() { return $this->param; } }
-- Sebastian Mendel www.sebastianmendel.de www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet

Sean Coates

21 years ago
> how can i check if a method is called statically when called from inside > another object? (without debug_bactrace())
<plug type="own">http://blog.phpdoc.info/archives/4-Schizophrenic-Methods.html</plug> Short answer: $isStatic = !(isset($this) && get_class($this) == __CLASS__); HTH S

Sebastian Mendel

21 years ago
Sean Coates schrieb:
>> how can i check if a method is called statically when called from inside >> another object? (without debug_bactrace()) > > <plug > type="own">http://blog.phpdoc.info/archives/4-Schizophrenic-Methods.html</plug> > > Short answer: > $isStatic = !(isset($this) && get_class($this) == __CLASS__);
i know this hack, but it does not work, if the statically called method is from the same class as the calling object class foo { function bar() { if ( isset( $this ) && get_class( $this ) == __CLASS__ ) { return 'not static'; } return 'static'; } function bar2() { return foo::bar(); } } // returns 'static' echo foo::bar(); // returns 'not static' but should be 'static' $foo = new foo; echo $foo->bar2();
-- Sebastian Mendel www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com www.sf.net/projects/phpdatetime www.sf.net/projects/phptimesheet

Sean Coates

21 years ago
> i know this hack, but it does not work, if the statically called method > is from the same class as the calling object
Good point. Sorry for not noticing. .. I have no idea WHY someone would want to do this.. that said, I don't know of a way to do it, either. S

Sebastian Mendel

21 years ago
Sean Coates wrote:
>> i know this hack, but it does not work, if the statically called method >> is from the same class as the calling object > > Good point. Sorry for not noticing. > ... I have no idea WHY someone would want to do this.. that said, I > don't know of a way to do it, either.
a function who returns a name of an object, say its name is $object->getName(); this needs that the object exists and loaded how about if i need a name for object not loaded? Class::getName( $object_id ); there is really no need to load/create the whole object if i only need the name, so doing $object = new Class( $object_id ); $object->getName(); would be a waste of system-resources f.e. the same applies to any other properties of an object stored in a DB. but why should i choose for the same function two different method-names? f.e. Class::getName() static Class::staticGetName() ?? this doesnt makes sence to me. pls, give me a hint if i had overseen something.
-- Sebastian Mendel www.sebastianmendel.de www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet

patrick müller

21 years ago
Sebastian Mendel wrote:
> > how about if i need a name for object not loaded? > > Class::getName( $object_id ); >
Weird, i thought you must know the classname for a static call :)

Sebastian Mendel

21 years ago
patrick müller wrote:
> Sebastian Mendel wrote: > >> how about if i need a name for object not loaded? >> >> Class::getName( $object_id ); >> > > Weird, i thought you must know the classname for a static > call :)
not the classname, the objectname f.e. the name for the object of type customer with id 31728 is 'Smith' ...
-- Sebastian Mendel www.sebastianmendel.de www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet

Andi Gutmans

21 years ago
I haven't quite understood what is wrong with instantiating the object. After all, it most probably reflects some external data (or the name wouldn't mean anything). Not creating the object itself won't save you anything. Anyway, you can still do Foo::getName($object_id), if that $object_id corresponds to some external resource like DB row. Then again, I probably misunderstood you because I don't quite understand the problem to begin with :) Andi At 08:59 AM 7/22/2005 +0200, Sebastian Mendel wrote:

Sebastian Mendel

21 years ago
Andi Gutmans wrote:
> I haven't quite understood what is wrong with instantiating the object.
i think its faster to just pull out the name (single value) from a database than the whole object (row or even rows from different tables)
> After all, it most probably reflects some external data (or the name > wouldn't mean anything). Not creating the object itself won't save you > anything. Anyway, you can still do Foo::getName($object_id), if that > $object_id corresponds to some external resource like DB row.
yes, but if someone calls Foo::getName() from inside an object who has also the propertie $foo->name this function would return this properties, but it should raise an user-error that there is missing a parameter of course, there is always a workaround - but why is $this available inside a statically called method?
> Then again, I probably misunderstood you because I don't quite > understand the problem to begin with :)
the main-question/problem is: "why is $this available inside a statically called method?" (PHP 4) and in PHP 5, the other point was, why should it not be possible to call one method statically and not statically? example, returning wrong $name: <?php // PHP 4 class Foo { var $name = 'name_of_foo'; function getName() { if ( isset( $this ) ) { return 'not static: ' . $this->name; } return 'static'; } function getParentName() { return Foo::getName(); } } // returns 'static' echo Foo::getName(); // returns 'not static' but should be 'static' $foo = new Foo; echo $foo->getParentName(); ?>
> Andi > > At 08:59 AM 7/22/2005 +0200, Sebastian Mendel wrote: >> Sean Coates wrote: >> >> i know this hack, but it does not work, if the statically called >> method >> >> is from the same class as the calling object >> > >> > Good point. Sorry for not noticing. >> > ... I have no idea WHY someone would want to do this.. that said, I >> > don't know of a way to do it, either. >> >> a function who returns a name of an object, say its name is >> >> $object->getName(); >> >> this needs that the object exists and loaded >> >> how about if i need a name for object not loaded? >> >> Class::getName( $object_id ); >> >> >> there is really no need to load/create the whole object if i only need >> the name, so doing >> >> $object = new Class( $object_id ); >> $object->getName(); >> >> would be a waste of system-resources >> >> f.e. the same applies to any other properties of an object stored in a >> DB. >> >> but why should i choose for the same function two different method-names? >> >> f.e. >> >> Class::getName() >> static Class::staticGetName() >> >> ?? >> >> this doesnt makes sence to me. >> >> pls, give me a hint if i had overseen something.
-- Sebastian Mendel www.sebastianmendel.de www.sf.net/projects/phpdatetime | www.sf.net/projects/phptimesheet