|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-02-08 00:12 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 06:00:01 2025 UTC |
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. :)