#49203 [Opn]: call_user_func_array when calling a parent constructor not from a user class

From: Date: Mon, 10 Aug 2009 07:48:19 +0000
Subject: #49203 [Opn]: call_user_func_array when calling a parent constructor not from a user class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-140839@lists.php.net to get a copy of this message
 ID:               49203
 Updated by:       colder@php.net
 Reported By:      magicaltux@php.net
 Status:           Open
 Bug Type:         Scripting Engine problem
 Operating System: Linux x86_64
 PHP Version:      5.3.0
 New Comment:

The problem is not about internal classes, but classes not defining a
__construct:

class A {

}
class B extends A {
    public function __construct() {
        echo "here\n";
        call_user_func(array('parent', '__construct'));
    }
}

$x = new B;

seems like is_callable() returns true on array('parent', '__construct')
and shouldn't.


Previous Comments:
------------------------------------------------------------------------

[2009-08-10 03:57:09] magicaltux@php.net

Description:
------------
When using:

call_user_func_array(array('parent', '__construct'), $var);

This works if the parent is a user-defined class, but not if it's an
extension-provided class (the extended constructor gets called twice).

This is not easy to explain, see attached reproduce code for more
details.


My initial code was (in a class extending mysqli):

        private function __construct($params) {
                call_user_func_array(array('parent', '__construct'),
$params);
                $this->set_charset('utf8');
        }

Using this instead awfully fixes the problem:
parent::__construct($params[0], $params[1], $params[2], $params[3]);

Note that this wasn't possible in PHP 5.2.x

Warning: call_user_func_array(): First argument is expected to be a
valid callback, 'parent::__construct' was given in foo.php on line 5


Reproduce code:
---------------
<?php

class B extends mysqli {
	public function __construct($var) {
		echo "here\n";
		call_user_func_array(array('parent', '__construct'), $var);
	}
}

$x = new B(array('localhost', 'root'));


Expected result:
----------------
here

Actual result:
--------------
here
here

Warning: call_user_func_array() expects parameter 2 to be array, string
given in foo.php on line 6



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=49203&edit=1



Thread (6 messages)

« previous php.bugs (#140839) next »