Edit report at https://bugs.php.net/bug.php?id=51176&edit=1
ID: 51176
Comment by: kusov26an at gmail dot com
Reported by: majkl578 at gmail dot com
Summary: Static calling in non-static method behaves like
$this->
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Irrelevant
PHP Version: 5.3.2RC3
Assigned To: felipe
Block user comment: N
Private report: N
New Comment:
Perhaps this behavior seems logical, although I don't understand why the :: operator in a
non-static context should refer to non-static methods. But then answer the question: how do I call
__callStatic in this context? After all, no one has provided such an opportunity
Previous Comments:
------------------------------------------------------------------------
[2016-04-13 20:43:49] kak dot serpom dot po dot yaitsam at gmail dot com
This is a real badass bug. Please explain why is this behaviour considered expected?
------------------------------------------------------------------------
[2013-08-17 13:59:52] daniel dot ruthardt at zoesolutions dot eu
For me, the real issue is to understand the :: operator, if you do, this is absolutely
no bug, but consistent and wanted behaviour.
To sum it all up, the :: operator tries to call the method in the current context
(static vs. non-static) whenever possible and assumes that the current context was
meant for non-existing methods.
----------
In details, the :: operator, does the following:
In a non-static context:
(1) Operand 1 is the context's class (via name, static or self)
(a) if the method is (defined) non-static, it is called non-static in the
current context
(b) if the method is (defined) static, it is called static
(c) if the method is not defined and __call is defined in the contextâs class,
__call is called (in the contextâs class)
(2) Operand 1 is a parent class of the context's class (via name or parent)
(a) if the method is (defined) non-static, it is called non-static in the
current context
(b) if the method is (defined) static, it is called static
(c) if the method is not defined and __call is defined in the contextâs class,
a fatal error is issued
(d) if the method is not defined and __call is defined in the parentâs class,
__call is called (in the parentâs class)
(e) if the method is not defined and __call is defined in the contextâs class
and in the parentâs class, __call is called in the contextâs class
(3) Operand 1 is a class not related to the context
(a) if the method is (defined) non-static, it is called non-static in the
current context (additionally a strict standards warning is issued)
(b) if the method is (defined) static, it is called static
(c) if the method is not defined and __callStatic is defined in the not related
class, __callStatic is called (in the not related class)
Observations:
(I) 2c and 2e are inconsistent. In 2c PHP wants to handle the issue in the parent
class, whereas in 2e suddenly the issue is handled in the contextâs class. I fail to
see consistent arguments here explaining both situations.
In a static context:
(1) Operand 1 is the contextâs class (via name, static or self)
(a) if the method is (defined) static, it is called static
(b) if the method is (defined) non-static, it is called static (additionally a
strict standards warning is issued)
(c) if the method is not defined and __callStatic is defined in the contextâs
class, __callStatic is called (in the contextâs class)
(2) Operand 1 is a parent class of the contextâs class (via name or parent)
(a) if the method is (defined) static, it is called static
(b) if the method is (defined) non-static, it is called static (additionally a
strict standards warning is issued)
(c) if the method is not defined and __callStatic is defined in the contextâs
class, a fatal error is issued
(d) if the method is not defined and __callStatic is defined in the parentâs
class, __callStatic is called (in the parentâs class)
(e) if the method is not defined and __callStatic is defined in the contextâs
and in the parentâs class, __call is called in the parentâs class
(3) Operand 1 is a class not related to the context
(a) if the method is (defined) static, it is called static
(b) if the method is (defined) non-static, it is called static (additionally a
strict standards warning is issued)
(c) if the method is not defined and __callStatic is defined in the not related
class, __callStatic is called (in the not related class)
Observations:
(I) As compared to the non-static context, 2c and 2e are consistent.
------------------------------------------------------------------------
[2011-07-29 15:57:20] steve at twitpic dot com
This is ridiculous- this bug is not bogus, it's completely legitimate, unexpected
behavior, and totally non documented. As a reasonable programmer, one expects a
static call to behave the same inside of a class as well as outside of a class.
How can we bring the "desired" behavior up for vote? I think that it's insane to
not fix it.
------------------------------------------------------------------------
[2011-05-31 02:06:16] david71rj at gmail dot com
Sorry by realive this topic, but I really think that it is a bug.
If I want call "bar" with context, the correct mean is $this->bar(). Else, the
"static" sounds for me like "call without context". I'm wrong?
Please, read this topic to understand what I'm saying:
http://stackoverflow.com/questions/6181603/php-is-handling-incorrectly-my-static-
call
------------------------------------------------------------------------
[2010-11-03 12:03:17] majkl578 at gmail dot com
You cannot call foo::start(), because the method is non-static and therefore it does not make sense.
Also, self, static and class name are used to call methods statically, not in a object context.
For calling method bar in object context, I think '$this->' should be used instead.
Obviously parent should not behave this way and respect context, but I think it should be the only
exception (as shown in #52713).
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=51176
--
Edit this bug report at https://bugs.php.net/bug.php?id=51176&edit=1