|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 06:00:02 2025 UTC |
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