Req #54897 [Opn->Wfx]: Allow class methods to determine whether they're being called statically or not

From: Date: Sun, 09 Mar 2014 23:19:16 +0000
Subject: Req #54897 [Opn->Wfx]: Allow class methods to determine whether they're being called statically or not
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184669@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54897&edit=1

 ID:                 54897
 Updated by:         requinix@php.net
 Reported by:        charles dot pick at gmail dot com
 Summary:            Allow class methods to determine whether they're
                     being called statically or not
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Unknown/Other Function
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Indeed: a method should be called on an instance or called statically, not both.


Previous Comments:
------------------------------------------------------------------------
[2014-03-09 17:19:10] phpmpan at mpan dot pl

It is already supported by checking if $this is set:

class Foo {
    public function bar() {
        var_dump(isset($this));
    }
}
Foo::bar();             // Prints bool(false) 
(new Foo())->bar();     // Prints bool(true)

However calling non-static methods as stacic is not ok and will produce strict warnings.

------------------------------------------------------------------------
[2011-05-21 10:44:00] charles dot pick at gmail dot com

Description:
------------
At the moment there is no reliable way to determine whether a function is being 
called statically or on an object directly. Sometimes it's useful to know the 
difference between e.g.
foo::createUrl();
and
$foo->createUrl();

it would be nice to have a function, called something like is_static_call() which 
could supply this information to the current method. I'm not familiar with the PHP 
internals so I don't know how difficult this is to implement, but for me it would 
be a useful feature.

Test script:
---------------
class foo {
	public function createUrl($url = null, $params = array()) {
		if ($url === null && is_static_call()) {
			$url = "list";
		}
		elseif ($url === null) {
			$url = "view";
		}
		/* do something */
	}
}



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



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


Thread (3 messages)

« previous php.bugs (#184669) next »