Req #43845 [Opn->Wfx]: Function can no longer be called both statically and as instance method

From: Date: Thu, 05 May 2016 11:41:40 +0000
Subject: Req #43845 [Opn->Wfx]: 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-200912@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 Updated by: nikic@php.net Reported by: ms419 at freezone dot co dot uk Summary: Function can no longer be called both statically and as instance method -Status: Open +Status: Wont fix Type: Feature/Change Request -Package: Feature/Change Request +Package: *General Issues PHP Version: 5.2.5 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2012-12-07 10:07:56] 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 ------------------------------------------------------------------------ [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

« previous php.bugs (#200912) next »