原始代码:
<?php
class ClassOne{
function callClassOne(){
print "In Class One\n";
}
}
class ClassTwo{
function callClassTwo(){
print "In Class Two\n";
}
}
class ClassOneDelegator{
private $target = array();
function __construct()
{
$this->target[] = new ClassOne();
}
function addObject($obj){
$this->target[] = $obj;
}
function __call($name, $arguments)
{
foreach($this->target as $obj){
$r = new ReflectionClass($obj);
if($method = $r->getMethod($name)){
if($method->isPublic() && !$method->isAbstract()){
return $method->invoke($obj, $args);
}
};
}
return false;
}
}
$objz = new ClassOneDelegator();
$objz->addObject(new ClassTwo());
$objz->callClassOne();
$objz->callClassTwo();
按照书中的代码运行会报以下错误,
D:\phpstudy_pro\WWW>php index.php
In Class One
Fatal error: Uncaught ReflectionException: Method callClassTwo does not exist in D:\phpstudy_pro\WWW\index.php on line 30
ReflectionException: Method callClassTwo does not exist in D:\phpstudy_pro\WWW\index.php on line 30
Call Stack:
0.0000 358608 1. {main}() D:\phpstudy_pro\WWW\index.php:0
0.0000 359152 2. ClassOneDelegator->callClassTwo() D:\phpstudy_pro\WWW\index.php:43
0.0000 359208 3. ClassOneDelegator->__call() D:\phpstudy_pro\WWW\index.php:43
0.0000 359320 4. ReflectionClass->getMethod() D:\phpstudy_pro\WWW\index.php:30
正常出来In Class One,但是In Class Two不能出来
原因在于这一行
if($method = $r->getMethod($name)){
ReflectionClass的实例$r 在执行getMethod方法时,首先会尝试调用ClassOne对象的相关方法,如果方法名称不存在,就会引发异常,直接就导致代码不能继续运行下去.所以考虑到加入异常处理以便跳过,使之能够调用下一个ClassTwo实例的callClassTwo方法.改进出下
<?php
class ClassOne{
function callClassOne(){
print "In Class One\n";
}
}
class ClassTwo{
function callClassTwo(){
print "In Class Two\n";
}
}
class ClassOneDelegator{
private $target = array();
function __construct()
{
$this->target[] = new ClassOne();
}
function addObject($obj){
$this->target[] = $obj;
}
function __call($name, $arguments)
{
// TODO: Implement __call() method.
foreach($this->target as $obj){
$r = new ReflectionClass($obj);
try{
if($method = $r->getMethod($name)){
if($method->isPublic() && !$method->isAbstract()){
return $method->invoke($obj, $args);
}
};
}catch (Exception $e){
//
}
}
return false;
}
}
$objz = new ClassOneDelegator();
$objz->addObject(new ClassTwo());
$objz->callClassOne();
$objz->callClassTwo();
运行的效果,达到书中的示例效果.
D:\phpstudy_pro\WWW>php z.php
In Class One
In Class Two
本文探讨了在PHP中实现类方法的动态调用机制,通过改进原始代码,实现了对多个类实例方法的安全调用,避免了因方法不存在引发的异常,确保了代码的稳定性和灵活性。
650

被折叠的 条评论
为什么被折叠?



