Doc #75530 [Com]: __callStatic precedence in non-static method invocation
| From: | daniel at ciochiu dot ro | Date: | Thu, 16 Nov 2017 04:11:17 +0000 |
| Subject: | Doc #75530 [Com]: __callStatic precedence in non-static method invocation | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15245@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75530&edit=1
ID: 75530
Comment by: daniel at ciochiu dot ro
Reported by: msaladna at apisnetworks dot com
Summary: __callStatic precedence in non-static method
invocation
Status: Open
Type: Documentation Problem
Package: Class/Object related
Operating System: Irrelevant
PHP Version: 7.1.11
Block user comment: N
Private report: N
New Comment:
The problem is that method test is already implemented and it is public (smaller code: https://3v4l.org/Gd1ba). Php documentation refers to overloading
logic (dynamically create properties and methods: http://php.net/manual/ro/language.oop5.overloading.php)
as:
<The overloading methods are invoked when interacting with properties or methods that have not
been declared or are not visible in the current scope. The rest of this section will use the terms
"inaccessible properties" and "inaccessible methods" to refer to this
combination of declaration and visibility.>
So, the test method is public.
If this method is changed to protected scope, you have the expected desire, which matches the
documentation: https://3v4l.org/AUtBB
Previous Comments:
------------------------------------------------------------------------
[2017-11-15 21:48:50] msaladna at apisnetworks dot com
Description:
------------
According to the documentation,
__callStatic() is triggered when invoking inaccessible methods in a static context.
If a method then is declared non-static with a __callStatic() handler in the class, accessing the
method statically should route through __callStatic() instead of generating a fatal error as the
method is inaccessible statically.
Test script:
---------------
<?php
class Test
{
protected $root = '';
public function __construct(string $root = '/')
{
$this->root = $root;
}
public static function bindTo(string $root = '/')
{
return new static($root);
}
public static function __callStatic($name, $arguments)
{
$instance = static::bindTo();
return $instance->$name($arguments);
}
public function test() {
return $this->root . '/foo';
}
}
print Test::bindTo('/elsewhere')->test();
print Test::test();
Expected result:
----------------
/elsewhere/foo
/foo
Actual result:
--------------
/elsewhere/foo
PHP Fatal error: Uncaught Error: Non-static method Test::test() cannot be called statically
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75530&edit=1