php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40398 parent and self callback functions erroneously called statically
Submitted: 2007-02-08 05:28 UTC Modified: 2007-02-08 08:43 UTC
From: levi at alliancesoftware dot com dot au Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2007-02-08 (CVS) OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: levi at alliancesoftware dot com dot au
New email:
PHP Version: OS:

 

 [2007-02-08 05:28 UTC] levi at alliancesoftware dot com dot au
Description:
------------
parent::func() uses a static function call syntax but is 'special' in that you are allowed to use it to call nonstatic functions without generating any warnings.

Callbacks with E_STRICT on don't recognise the 'special' nature of 'parent' and 'self'. (happens with call_user_func(), call_user_func_array(), array_map() etc)

This means that it is impossible to call a parent method through a callback without generating a warning.


Therefore, code such as:

public function __construct(/* variable argument list */) {
  $args = func_get_args();
  call_user_func_array('parent', '__construct), $args);

  /* ... extra initialisation here ... */
}

will not work with E_STRICT



Note: Related to #36011, but not quite the same (this one involves inheritance)

Reproduce code:
---------------
#!/usr/local/src/php5/php-5.2.CVS-cgi/sapi/cgi/php -q
<?
error_reporting(E_ALL | E_STRICT);


class Ancestor {
    public function f($a) {
        echo $a;
    }
}

class Descendant extends Ancestor {
    public function f($a) {
    }

    public function g($a) {
        // This is fine
        parent::f($a);

        // This gives a warning
        call_user_func(array('parent', 'f'), $a);

        // This is fine
        self::f($a);

        // This gives a warning
        call_user_func(array('self', 'f'), $a);

        // This is fine
        Descendant::f(4);

        // This gives a warning [probably not avoidable]
        call_user_func(array('Descendant', 'f'), $a);
    }
}

$x = new Descendant();
$x->g(3);

?>

Expected result:
----------------
33

Actual result:
--------------
PHP Strict Standards:  Non-static method [...] cannot be called statically, assuming $this from compatible context Descendent in /home/levi/public_html/test2.php5 on line [...]

(See code for which lines generate the warning)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-08 08:43 UTC] helly@php.net
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

Use: call_user_func_array(array($this, \'parent::__construct\'), $args);
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 03:01:33 2025 UTC