《php操作類與對(duì)象的函數(shù)二》要點(diǎn):
本文介紹了php操作類與對(duì)象的函數(shù)二,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
class_alias為類創(chuàng)建一個(gè)別名
格式:bool class_alias(str $original,str $alias[,bool $autoload=TRUE])
class foo { }
class_alias('foo', 'bar');
此時(shí)foo與bar這兩個(gè)類一樣
get_called_class獲取類的名稱,靜態(tài)方法被調(diào)用,無參數(shù),類內(nèi)使用
格式:string get_called_class ( void )
class foo {
static public function test() {
var_dump( get_called_class() );
}
}
class bar extends foo {}
foo::test();//foo
bar::test();//bar
get_class_vars獲取可以從當(dāng)前范圍訪問的類的默認(rèn)屬性
格式:array get_class_vars ( string $class_name )
class myclass {
var $var1; //無默認(rèn)值
var $var2 = "xyz";
var $var3 = 100;
private $var4;
function __construct() {
// change some properties
$this->var1 = "foo";
$this->var2 = "bar";
return true;
}
}
$my_class = new myclass();
print_r( get_class_vars(get_class($my_class)) );
print_r( get_object_vars($my_class) );
輸出:
Array(
[var1] =>
[var2] => xyz
[var3] => 100
)
Array(
[var1] => foo
[var2] => bar
[var3] => 100
)
get_declared_interfaces返回所有已聲明的接口的數(shù)組
格式:array get_declared_interfaces ( void )
interface haha{}
print_r(get_declared_interfaces());
get_declared_traits返回所有已聲明為trait的數(shù)組
格式:array get_declared_traits ( void )
trait Trait1 {
public function hello() {}
}
trait Trait2 {
public function hi() {}
}
print_r(get_declared_traits());
interface_exists檢查接口是否已定義
格式:bool interface_exists (str $interface_name [,bool $autoload = true ] )
if (interface_exists('Myface')) {
class MyClass implements Myface
{ // Methods }
}
is_subclass_of檢查對(duì)象是否有這個(gè)類作為它的一個(gè)父類或?qū)崿F(xiàn)它
格式:bool is_subclass_of ( mixed $object , string $class_name [, bool $allow_string = TRUE ] )
class Factory{
var $oink = 'father';
}
class F_Child extends Factory{
var $oink = 'child';
}
if (is_subclass_of('F_Child', 'Factory')) {
echo "是, F_Child 有父類 Factory\n";
} else {
echo "不, F_Child 沒有父類Factory\n";
}
輸出:是, F_Child 有父類 Factory
trait_exists檢查trait類是否存在
格式:bool trait_exists ( string $traitname [, bool $autoload ] )
trait World {
private static $instance;
protected $tmp;
public static function World(){
//將(hello)對(duì)象賦值給靜態(tài)屬性
self::$instance = new static();
self::$instance->tmp = get_called_class().' '.__TRAIT__;
return self::$instance;//返回一個(gè)對(duì)象
}
}
if ( trait_exists( 'World' ) ) {
class Hello {
use World;
public function text( $str ){
return $this->tmp.$str;
}
}
}
echo Hello::World()->text('!!!'); // Hello World!!!
《php操作類與對(duì)象的函數(shù)二》是否對(duì)您有啟發(fā),歡迎查看更多與《php操作類與對(duì)象的函數(shù)二》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/14282.html