Doc #75530 [Opn->Nab]: __callStatic precedence in non-static method invocation
| From: | jhdxr@php.net | Date: | Wed, 26 Dec 2018 16:16:53 +0000 |
| Subject: | Doc #75530 [Opn->Nab]: __callStatic precedence in non-static method invocation | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16229@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
Updated by: jhdxr@php.net
Reported by: msaladna at apisnetworks dot com
Summary: __callStatic precedence in non-static method
invocation
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Class/Object related
Operating System: Irrelevant
PHP Version: 7.1.11
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
this is expected behavior, see daniel's explanation above.
Previous Comments:
------------------------------------------------------------------------
[2017-11-16 04:11:09] daniel at ciochiu dot ro
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
------------------------------------------------------------------------
[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