Req #43845 [Com]: Function can no longer be called both statically and as instance method

From: Date: Fri, 07 Dec 2012 10:07:56 +0000
Subject: Req #43845 [Com]: Function can no longer be called both statically and as instance method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-175193@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=43845&edit=1

 ID:                 43845
 Comment by:         php dot net at site dot lanzz dot org
 Reported by:        ms419 at freezone dot co dot uk
 Summary:            Function can no longer be called both statically and
                     as instance method
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 PHP Version:        5.2.5
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2011-08-09 03:20:10] 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.

------------------------------------------------------------------------
[2008-01-14 20:15:20] 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...


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=43845&edit=1


Thread (4 messages)

« previous php.bugs (#175193) next »