|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2008-01-14 20:15 UTC] ms419 at freezone dot co dot uk
 Description:
------------
I understand that, unlike some other languages, PHP does not support overloading: I can't implement two functions with the same name but different signatures. However I can simulate overloading using func_get_args() and testing with which arguments the function was called.
Now what I want is a function which can be called either as an instance method with no arguments, or statically with one argument: an instance of the class. I test whether the function was called statically or not using isset($this)
However in PHP5, this produces an error:
Non-static method BaseTaxonomy::getTerms() should not be called statically in...
Like it is possible to simulate overloading in PHP without generating errors, I wish it were possible to define a function which can be called either statically or as an instance method, without generating errors.
Much thanks, Jack
Reproduce code:
---------------
Toy example:
class BaseTaxonomy
{
  protected $terms = null;
  public function getTerms()
  {
    if (!isset($this))
    {
      $args = func_get_args();
      return $args[0]->terms;
    }
    return $this->terms;
  }
}
Actual result:
--------------
Non-static method BaseTaxonomy::getTerms() should not be called statically in...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 17:00:02 2025 UTC | 
Actually, you must declare your method static. It will not produce any warning, but you will cannot be able to use $this. Instead, you must always use the first parameter. So this will be like this: <?php class MyClass { static public function MyMethod() { $this = $Instance = func_get_arg(0); if ( ! $this instanceof self ) { throw new Exception('You must use this method with an Instance of the same class.'); } /* ... your code here ... */ } } ?> Remember.. You will always need to pass the instance for your method to work correctly. Farewell.Since PHP does actually distinguish between static and non-static methods, it makes no sense (as external interface) to disallow _both_ calling the same method as static and non-static _and_ having same-named static and non-static methods. At run time PHP knows if you're calling a static or a non-static method, so it can pick the correct one. Here is an example where same-named static and non-static methods might make sense: class Foo { static public function defaultInstance() { static $instance = null; if (is_null($instance)) { $instance = new self(); } } static public function bar() { $instance = self::defaultInstance(); return $instance->bar(); } public function bar() { // do something } } Foo::bar() // no need to explicitly request the default instance $f = new Foo(); $f->bar() // but now it is clear that we're doing the same thing with a specific instanceRegarding the last comment, PHP cannot allow a static and non-static method with the same name, because this would create an ambiguity between static calls and scoped instance calls. Consider: class A { public function method() {} public static function method() {} } class B extends A { public function test() { parent::method(); } } Current parent::method() is a scoped instance call to non-static A::method(). Think of it as doing $this->method() but forcing a scope of "parent". If there could be a static and a non-static method with the same name, parent::method() could also refer to a static call to static A::method(), thus causing an ambiguity. In either case, the "you can call a method either statically or not" functionality is on the way out, so I'm closing this as Won't Fix.