PHP 8.5.0 Alpha 1 available for testing

Voting

: three plus two?
(Example: nine)

The Note You're Voting On

Daniel Smith
14 years ago
Be careful of __call in case you have a protected/private method. Doing this:

<?php
class TestMagicCallMethod {
public function
foo()
{
echo
__METHOD__.PHP_EOL;
}

public function
__call($method, $args)
{
echo
__METHOD__.PHP_EOL;
if(
method_exists($this, $method))
{
$this->$method();
}
}

protected function
bar()
{
echo
__METHOD__.PHP_EOL;
}

private function
baz()
{
echo
__METHOD__.PHP_EOL;
}
}

$test = new TestMagicCallMethod();
$test->foo();
/**
* Outputs:
* TestMagicCallMethod::foo
*/

$test->bar();
/**
* Outputs:
* TestMagicCallMethod::__call
* TestMagicCallMethod::bar
*/

$test->baz();
/**
* Outputs:
* TestMagicCallMethod::__call
* TestMagicCallMethod::baz
*/
?>

..is probably not what you should be doing. Always make sure that the methods you call in __call are allowed as you probably dont want all the private/protected methods to be accessed by a typo or something.

<< Back to user notes page

To Top