我在我的类User中定义了一个方法setAddress($town,$zip.$coord)。在同一个类用户中,我有一个__call setter 'set',当我的方法被调用时只使用一个参数(例如:setAddress($town))。问题是当我使用一个参数调用该方法时:setAddress('New York'),我有一个错误('Missing parameters')。如果我用 3 个参数调用它,则重载正在工作。如果使用 1 个参数调用方法,为什么不调用 __call 函数?
用户.php
namespace com\killerphp\modells;
class User{
protected $address;
protected $firstName;
protected $lastName;
protected $email;
public function setAddress($town,$zip,$coord){
echo "I have 3 arguments";
}
public function __call($name, $arguments) {
$prefix= substr($name, 0, 3); //get,set
$property=substr($name, 3); //address,firstName,email etc
$property=lcfirst($property);
switch($prefix){
case "set":
if(count($arguments)==1){
echo 'asa i';
$this->$property=$arguments[0];
}
break;
case "get":
return $this->$property;
break;
default: throw new \Exception('magic method doesnt support the prefix');
}
}
}
索引.php
define('APPLICATION_PATH', realpath('../'));
$paths=array(
APPLICATION_PATH,
get_include_path()
);
set_include_path(implode(PATH_SEPARATOR,$paths));
function __autoload($className){
$filename=str_replace('\\',DIRECTORY_SEPARATOR , $className).'.php';
require_once $filename;
}
use com\killerphp\modells as Modells;
$g=new Modells\User();
$g->setAddress('new york','23444','west');
echo($g->getAddress());