Re: [PHP-DEV] $this availability inside static-functions

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

 



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

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