Req #54897 [Com]: Allow class methods to determine whether they're being called statically or not
Edit report at https://bugs.php.net/bug.php?id=54897&edit=1
ID: 54897
Comment by: phpmpan at mpan dot pl
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
Type: Feature/Change Request
Package: Unknown/Other Function
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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)