本文实例讲述了PHP面向对象程序设计之类与反射API。,具体如下:
了解类
class_exists验证类是否存在
<?php
// TaskRunner.php
$classname = "Task";
$path = "tasks/{$classname}.php";
if ( ! file_exists( $path ) ) {
throw new Exception( "No such file as {$path}" ); //抛出异常,类文件不存在
}
require_once( $path );
$qclassname = "tasks$classname";
if ( ! class_exists( $qclassname ) ) {
throw new Exception( "No such class as $qclassname" ); //抛出异常,类不存在Fatal error: Uncaught exception 'Exception' with message 'No such class as tasksTask'
Stack trace:
#0 {main}
}
$myObj = new $qclassname();
$myObj->doSpeak();
?>
get_class 检查对象的类 instanceof 验证对象是否属于某个类
<?php
class CdProduct {}
function getProduct() {
return new CdProduct( "Exile on Coldharbour Lane",
"The", "Alabama 3", 10.99, 60.33 ); // 返回一个类对象
}
$product = getProduct();
if ( get_class( $product ) == 'CdProduct' ) {
print "$product is a CdProduct objectn";
}
?>
<?php
class CdProduct {}
function getProduct() {
return new CdProduct( "Exile on Coldharbour Lane",
"The", "Alabama 3", 10.99, 60.33 );
}
$product = getProduct();
if ( $product instanceof CdProduct ) {
print "$product is a CdProduct objectn";
}
?>
get_class_methods 得到类中所有的方法列表,只获取public的方法,protected,private的方法获取不到。默认的就是public。
<?php
class CdProduct {
function __construct() { }
function getPlayLength() { }
function getSummaryLine() { }
function getProducerFirstName() { }
function getProducerMainName() { }
function setDiscount() { }
function getDiscount() { }
function getTitle() { }
function getPrice() { }
function getProducer() { }
}
print_r( get_class_methods( 'CdProduct' ) );
?>
output:
Array ( [0] => __construct [1] => getPlayLength [2] => getSummaryLine [3] => getProducerFirstName [4] => getProducerMainName [5] => setDiscount [6] => getDiscount [7] => getTitle [8] => getPrice [9] => getProducer )
更多验证
<?php
class ShopProduct {}
interface incidental {};
class CdProduct extends ShopProduct implements incidental {
public $coverUrl;
function __construct() { }
function getPlayLength() { }
function getSummaryLine() { }
function getProducerFirstName() { }
function getProducerMainName() { }
function setDiscount() { }
function getDiscount() { }
function getTitle() { return "titlen"; }
function getPrice() { }
function getProducer() { }
}
function getProduct() {
return new CdProduct();
}
$product = getProduct(); // acquire an object
$method = "getTitle"; // define a method name
print $product->$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 ShopProductn";
}
if ( is_subclass_of( $product, 'incidental' ) ) {
print "CdProduct is a subclass of incidentaln";
}
if ( in_array( 'incidental', class_implements( $product )) ) {
print "CdProduct is an interface of incidentaln";
}
?>







