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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 02:01:30 2024 UTC