php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47100 Magic method __call() does not work when class is named __call
Submitted: 2009-01-14 15:11 UTC Modified: 2009-01-15 09:59 UTC
From: dreadlabs at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.8 OS: Ubuntu
Private report: No CVE-ID: None
 [2009-01-14 15:11 UTC] dreadlabs at gmail dot com
Description:
------------
I created a class named __call, within that class i created the magic method __call. I then instantiated the __call class and tried to call a non-existent method. I then got a missing argument error from PHP, instead of it using the __call method.

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

class __call {

	public function __call($method, $args){
		echo $method."\n";
	}

}

$obj = new __call;
$obj->test();

?>

Expected result:
----------------
test

Actual result:
--------------
Warning: Missing argument 1 for __call::__call(), called in /home/tam/test.php on line 15 and defined in /home/tam/test.php on line 5

Warning: Missing argument 2 for __call::__call(), called in /home/tam/test.php on line 15 and defined in /home/tam/test.php on line 5

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-14 17:42 UTC] crrodriguez at opensuse dot org
Interesting.. in reality it IMHO should abort with a fatal error when classes are named __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state and __clone.
 [2009-01-14 17:54 UTC] dreadlabs at gmail dot com
Personally, i dont see why you would raise a fatal error when using magic methods for class names, you're not conflicting with anything, it should allow you to use it as normal.
 [2009-01-15 03:02 UTC] typoon at gmail dot com
Think about it for a minute. If you class is called '__call' and you declare a function '__call', that one would be the constructor right? The error you experience is that you are calling the constructor without the parameters it expects.
Check this code:

<?php

class __call {

	public function __call($method, $args){
		echo $method."\n";
	}

    public function test() {
        echo "i am test\n";
    }

}

$obj = new __call('my method','arguments');
$obj->test();

?>

So, either PHP should not allow a class to have the same name as magic methods, or you need to play with the idea that the constructor comes first.
Other 'workaround' for this would be:

<?php

class __call {

    public function __construct(){}

	public function __call($method, $args){
		echo $method."\n";
	}

    public function test() {
        echo "i am test\n";
    }

}

$obj = new __call('my method','arguments');
$obj->test();

$obj2 = new __call;

?>

Now you have a __construct() function, so __call can be used normally.

Regards,

Henrique
 [2009-01-15 09:59 UTC] dreadlabs at gmail dot com
Henrique,

Thanks for that, i completely forgot about the php4 backwards compatibility. I havent used php4 in so long that it totally slipped my mind.

Bug set as bogus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC