Home > Backend Development > PHP Tutorial > PHP对象新增方法的有关问题

PHP对象新增方法的有关问题

WBOY
Release: 2016-06-13 12:20:42
Original
911 people have browsed it

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 />
Copy after login


因为一些原因,我希望使用的时候可以是这样的:
<br /><br />$test = new MyClass();<br />$test->b();<br /><br />
Copy after login



就“好像”是这样:


class MyClass
{
funciton a(){
echo 'a';
}
}


class MyClass extends MyClass
{
function b(){
echo 'b';
}
}<br /><br />$test = new MyClass();<br />$test->b();<br /><br />
Copy after login


但这显示不对的,新extends的类名必须与父类不同,那么问题来了?

有什么办法可以直接对一个类新增一个方法吗?

------解决思路----------------------
没有这种写法,类名不能重复定义
为什么不在MyClass类里面写方法b() 呢,MyClassExted?继承 MyClass后,可以重写MyClass 的方法b()
<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 />
Copy after login

------解决思路----------------------
你可以给类预留一个接口呀
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 />
Copy after login

------解决思路----------------------
<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 />
Copy after login

------解决思路----------------------
再给你个加强升级版
<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 />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template