php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43845 Function can no longer be called both statically and as instance method
Submitted: 2008-01-14 20:15 UTC Modified: 2016-05-05 11:41 UTC
Votes:2
Avg. Score:2.5 ± 1.5
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ms419 at freezone dot co dot uk Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.5 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ms419 at freezone dot co dot uk
New email:
PHP Version: OS:

 

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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-09 03:20 UTC] klaussantana at gmail dot com
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.
 [2012-12-07 10:07 UTC] php dot net at site dot lanzz dot org
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 
instance
 [2016-05-05 11:41 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2016-05-05 11:41 UTC] nikic@php.net
Regarding 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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 20:01:46 2025 UTC