了解类
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 tasks\task'
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 object\n";
}
?>
<?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 object\n";
}
?>
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 "title\n"; }
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 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:
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方法
<?php
class othershop {
function thing() {
print "thing\n";
}
function andanotherthing() {
print "another thing\n";
}
}
class delegator {
private $thirdpartyshop;
function construct() {
$this->thirdpartyshop = new othershop();
}
function call( $method, $args ) { // 当调用未命名方法时执行call方法
if ( method_exists( $this->thirdpartyshop, $method ) ) {
return $this->thirdpartyshop->$method( );
}
}
}
$d = new delegator();
$d->thing();
?>
output:
thing
传参使用
<?php
class othershop {
function thing() {
print "thing\n";
}
function andanotherthing( $a, $b ) {
print "another thing ($a, $b)\n";
}
}
class delegator {
private $thirdpartyshop;
function construct() {
$this->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:
another thing (hi, hello)
以上就是php面向对象中一些类的代码实例汇总的详细内容。