Bug #75933 [Fbk->Csd]: call_user_func_array does not resolve parent for instances
| From: | jason at papertower dot com | Date: | Fri, 16 Feb 2018 20:53:32 +0000 |
| Subject: | Bug #75933 [Fbk->Csd]: call_user_func_array does not resolve parent for instances | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-213996@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75933&edit=1
ID: 75933
User updated by: jason at papertower dot com
Reported by: jason at papertower dot com
Summary: call_user_func_array does not resolve parent for
instances
-Status: Feedback
+Status: Closed
Type: Bug
Package: Class/Object related
Operating System: MacOS 10.13.3
PHP Version: 7.1.14
Block user comment: N
Private report: N
New Comment:
Thanks for the feedback, I apparently missed a subtlety in this. This code better reflects the case:
class Parent {
function A() {}
}
class Child extends Parent {
static function B() {
call_user_func_array('parent::A'); // problem
}
}
$child = new Child();
$child->B();
---
The important difference here is that we're attempting to call a parent instance method from a
child static method. We are calling it from a child instance, but the context is now static (since
we're in a static method).
As I've thought through this, I think the pattern is bizarre in itself. It does present the
fact that there's no discreet class versus instance identifier (e.g. self vs $this) for a
parent, but I don't think a parent instance keyword even makes sense. What would it even
describe?
Changing the child method from static to instance resolves the issue, and there's certainly no
bug here, so I'm closing this. It was a fascinating trip in OOP, though. :)
Previous Comments:
------------------------------------------------------------------------
[2018-02-08 00:23:45] daverandom@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php
If you can provide more information, feel free to add it
to this bug and change the status back to "Open".
Thank you for your interest in PHP.
Once I fixed the two unrelated errors in your example (a class cannot be named "Parent"
and call_user_func_array requires a second argument), this seems to work without issue: https://3v4l.org/huQ0d
Can you create a script which directly reproduces the error you encountered?
Thanks
------------------------------------------------------------------------
[2018-02-08 00:14:24] jason at papertower dot com
Fixed typo in summary
------------------------------------------------------------------------
[2018-02-08 00:12:11] jason at papertower dot com
Description:
------------
When working within a child class, attempting to call a parent instance method using
call_user_func_array is impossible. The function will always interpret the callable as a static
method, rather than an instance.
I apologize if this is in the wrong place or if not enough information was provided. It seems like a
simple, reproducible bug. It makes sense, as I'm not sure how the function would discern the
parent. Perhaps there needs to be an explicit equivalent of $this but for the parent?
Test script:
---------------
class Parent {
function A() {}
}
class Child extends Parent {
function B() {
call_user_func_array('parent::A');
}
}
$child = new Child();
$child->B();
Expected result:
----------------
I would expect the code to be the equivalent of running parent::A() within the method, resolving
based on context whether the parent is an instance or class.
Actual result:
--------------
An error is throwing saying the callable is not a valid callback as a non-static method was
expected, and an instance method should not be called statically.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75933&edit=1