php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75933 call_user_func_array does not resolve parent for instances
Submitted: 2018-02-08 00:12 UTC Modified: 2018-02-16 20:53 UTC
From: jason at papertower dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 7.1.14 OS: MacOS 10.13.3
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-02-08 00:14 UTC] jason at papertower dot com
-Summary: call_user_func_array does not resolve parent for instnaces +Summary: call_user_func_array does not resolve parent for instances
 [2018-02-08 00:14 UTC] jason at papertower dot com
Fixed typo in summary
 [2018-02-08 00:23 UTC] daverandom@php.net
-Status: Open +Status: Feedback
 [2018-02-08 00:23 UTC] 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-16 20:53 UTC] jason at papertower dot com
-Status: Feedback +Status: Closed
 [2018-02-16 20:53 UTC] jason at papertower dot com
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. :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 08:01:33 2024 UTC