📄 07c09-1.php
字号:
<?php// Define a class that will overload a method definitionclass Overloaded_Math { // Define an addition function that handles many different variations: public function __call($name, $args) { // If the function name was 'add' then: if ($name == 'add') { // If the first argument is a string: if (is_string($args[0])) { // Concatenate all the strings together: return implode('', $args); } elseif ((count($args) == 1) && is_array($args[0])) { // Ok we have exactly one argument, and it's an array // so sum up the values of the array: return array_sum($args[0]); } elseif ((count($args) > 1) && is_numeric($args[0])) { // If there are more than 1 arguments, and the // first one at least is numeric, then add them all: return array_sum($args); } } // In any other case, we didn't have a proper method call, exit trigger_error('Invalid Method Access', E_USER_ERROR); }}// Create a new object$mobj = new Overloaded_Math();// Examine the output of calling this method in multiple ways:// As strings: 'RedWhiteBlue'echo '<p>Accessed as strings: ', $mobj->add('Red', 'White', 'Blue'), '</p>';// As an array of numbers: 16echo '<p>Accessed as array: ', $mobj->add(array(7, 6, 3)), '</p>';// And as a bunch of numbers: 42echo '<p>Accessed as individual numbers: ', $mobj->add(37.83, 3.17, 1), '</p>';?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -