Re: $this availability inside static-functions

From: Date: Thu, 21 Jul 2005 19:35:36 +0000
Subject: Re: $this availability inside static-functions
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-219143@lists.php.net to get a copy of this message
Sean Coates wrote:
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 In the case of his example this will not work, as the static function call is coming from the same class,
which probably doesn't happen in real code, but this isn't a complete solution(I don't know that there is one). But to answer the other question:
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 A static method is a static method. You should call it statically. It has no meaning as an instance
method, because the method returns the same results regardless of object state. So, even if you have an instance of an object, you should still call the method statically. Then again, it is a notice, which could safely be ignored(although I don't like to).
but PHP doesnt support multiple function-definitions like: class foo { static function bar() {
       return ANY_DEFAULT_VALUE;
} function bar() {
       return $this->value;
} }
This seems like it might be useful, but I don't know how possible/clean this is. It would solve your problem though. I imagine someone out there has something to say about how this should be done. I assume you want to allow users of the class to use the class statically, and use default values, or use an instance of the class to change defaults. What is the best way to get this done. And I think I'll move this reply to php-generals instead of here. John LeSueur

« previous php.general (#219143) next »