了解类
class_exists验证类是否存在
doSpeak(); ?>
get_class 检查对象的类 instanceof 验证对象是否属于某个类
get_class_methods 得到类中所有的方法列表,只获取public的方法,protected,private的方法获取不到。默认的就是public。
output:
立即学习“PHP免费学习笔记(深入)”;
Array ( [0] => construct [1] => getPlayLength [2] => getSummaryLine [3] => getProducerFirstName [4] => getProducerMainName [5] => setDiscount [6] => getDiscount [7] => getTitle [8] => getPrice [9] => getProducer )
更多验证
$method(); // invoke the method
if ( in_array( $method, get_class_methods( $product ) ) ) {
print $product->$method(); // invoke the method
}
if ( is_callable( array( $product, $method) ) ) {
print $product->$method(); // invoke the method
}
if ( method_exists( $product, $method ) ) {
print $product->$method(); // invoke the method
}
print_r( get_class_vars( 'CdProduct' ) );
if ( is_subclass_of( $product, 'ShopProduct' ) ) {
print "CdProduct is a subclass of ShopProduct\n";
}
if ( is_subclass_of( $product, 'incidental' ) ) {
print "CdProduct is a subclass of incidental\n";
}
if ( in_array( 'incidental', class_implements( $product )) ) {
print "CdProduct is an interface of incidental\n";
}
?>output:
立即学习“PHP免费学习笔记(深入)”;
title title title title Array ( [coverUrl] => ) CdProduct is a subclass of ShopProduct CdProduct is a subclass of incidental CdProduct is an interface of incidental
call方法
ThinkPHP是一个快速、简单的基于MVC和面向对象的轻量级PHP开发框架,遵循Apache2开源协议发布,从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,尤其注重开发体验和易用性,并且拥有众多的原创功能和特性,为WEB应用开发提供了强有力的支持。 3.2版本则在原来的基础上进行一些架构的调整,引入了命名空间支持和模块化的完善,为大型应用和模块化开发提供了更多的便利。
thirdpartyShop = new OtherShop();
}
function call( $method, $args ) { // 当调用未命名方法时执行call方法
if ( method_exists( $this->thirdpartyShop, $method ) ) {
return $this->thirdpartyShop->$method( );
}
}
}
$d = new Delegator();
$d->thing();
?>output:
立即学习“PHP免费学习笔记(深入)”;
thing
传参使用
thirdpartyShop = new OtherShop();
}
function call( $method, $args ) {
if ( method_exists( $this->thirdpartyShop, $method ) ) {
return call_user_func_array(
array( $this->thirdpartyShop,
$method ), $args );
}
}
}
$d = new Delegator();
$d->andAnotherThing( "hi", "hello" );
?>output:
立即学习“PHP免费学习笔记(深入)”;
another thing (hi, hello)










