<?php
class OverloadedMethodTest {
public function __call($name, $arguments) {
// The value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments)。 "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments) {
// The value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments)。 "\n";
}
}
$obj = new OverloadedMethodTest;
$obj->runOverloadedTest('in an object context');
OverloadedMethodTest::runOverloadedTest('in a static context'); // As of PHP 5.3.0
?>
清单 4 中的代码产生以下输出:
Calling object method 'runOverloadedTest' in an object context
Calling static method 'runOverloadedTest' in a static context
在清单 4 中,注意重载的代码是如何根据调用代码来推断方法名和参数的:
$obj->runOverloadedTest('in an object context');
OverloadedMethodTest::runOverloadedTest('in a static context'); // As of PHP 5.3.0
<?php
$beverage = 'coffee';
// The following works; "'" is an invalid character for variable names
echo "$beverage's taste is great";
// The following won't work; 's' is a valid character for variable names but the
echo "He drank a number of $beverages";
variable is "$beverage"
echo "He drank some ${beverage}s"; // works
echo "He drank some {$beverage}s"; // works
?>
清单 5 中的代码产生以下输出:
coffee's taste is great
He drank a number of
He drank some coffees
He drank some coffees