Home > Article > Backend Development > PHP对象新增方法的有关问题
PHP对象新增方法的问题
很普通的写法:
<br /><br />class MyClass<br />{<br /> funciton a(){<br /> echo 'a';<br /> }<br />}<br /><br /><br />class MyClassExted extends MyClass<br />{<br /> function b(){<br /> echo 'b';<br /> }<br />}<br /><br />$test = new MyClassExted();<br />$test->b();<br /><br /><br />
<br /><br />$test = new MyClass();<br />$test->b();<br /><br />
class MyClass
{
funciton a(){
echo 'a';
}
}
class MyClass extends MyClass
{
function b(){
echo 'b';
}
}<br /><br />$test = new MyClass();<br />$test->b();<br /><br />
<br />class MyClass<br />{<br /> function a(){<br /> echo 'a';<br /> }<br /> function b(){<br /> echo 'MyClass b';<br /> }<br />}<br />class MyClassExtend extends MyClass<br />{<br /> function b(){<br /> echo 'MyClassExtend b';<br /> }<br />}<br /> <br />$MyClass = new MyClass();<br />$MyClass->b();//MyClass b<br /><br />$MyClassExtend = new MyClassExtend();<br />$MyClassExtend->b();//MyClassExtend b<br />
class MyClass<br />{<br /> funciton a(){<br /> echo 'a';<br /> }<br /><br /> function __call($name, $param) {<br /> if(function_exists($name)) {<br /> return call_user_func_array($name, $param);<br /> }<br /> }<br />}<br /><br />function b() {<br /> echo $b;<br />}<br /><br />$test = new MyClass();<br />$test->b();<br />
<br />class MyClass<br />{<br /> function a(){<br /> echo 'a';<br /> }<br /><br /> function __call($name, $param){<br /> if($name=='b'){<br /> echo 'b';<br /> }<br /> }<br />}<br /><br />$obj = new MyClass();<br />$obj->b();<br />
<br /><?php<br />class api_base<br />{<br /> function a(){<br /> echo 'a111';<br /> }<br /> function __call($name, $param) {<br /> static $methods = array();<br /> if(!method_exists($this, $name)){<br /> list($api, $fun) = explode('_',$name);<br /> empty($fun) && $fun = 'index';<br /> if(!isset($methods[$api])){<br /> include_once('class.api.'.$api.'.php');<br /> $method = 'api_'.$api;<br /> $methods[$api] = new $api();<br /> }<br /> if(!method_exists($methods[$api], $fun)){<br /> die('error');<br /> }<br /> call_user_func_array(array($methods[$api], $fun), $param);<br /> }<br /> }<br />}<br /> <br />$test = new api_base();<br />$test->b(1,2,3);<br />$test->b(4,5,6);<br /><br />$test->b_test(1,2,3);<br />$test->b_haha(4,5,6);<br />?><br />class.api.b.php:<br /><?php<br />class api_b extends api_base<br />{<br /> function index($a,$b,$c){<br /> echo "$a - $b - $c <br>";<br /> }<br /> function test($a,$b,$c){<br /> echo "$a - $b - $c <br>";<br /> }<br /> function haha($a,$b,$c){<br /> echo "$a - $b - $c <br>";<br /> }<br />}<br />?><br />