您好,登錄后才能下訂單哦!
今天小編給大家分享的是關(guān)于PHP函數(shù)庫之類與對象的詳解,相信很多人都不太了解,為了讓大家更加了解PHP函數(shù)庫之類與對象,所以給大家總結(jié)了以下內(nèi)容,一起往下看吧。一定會有所收獲的哦。
廢棄
一些函數(shù)已經(jīng)被廢棄或者移除,請不要使用它們
__autoload - 7.2 版本廢棄
call_user_method_array - 7.0 版本移除
call_user_method - 7.0 版本移除
判斷
類的存在性檢查
相關(guān)函數(shù)
class_exists - 判斷類是否存在
interface_exists - 判斷接口是否存在
trait_exists - 判斷 Trait 是否存在
第二個參數(shù)用來決定如果尚未加載,是否使用自動加載。
class_exists ( string $class_name [, bool $autoload = true ] ) : bool interface_exists ( string $interface_name [, bool $autoload = true ] ) : bool trait_exists ( string $traitname [, bool $autoload = true ] ) : bool
示例 - 廣泛的類存在性檢查函數(shù)
function common_class_exists(string $class): bool { return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false); }
類成員的存在性檢查
相關(guān)函數(shù):
property_exists - 檢查屬性是否存在
method_exists — 檢查方法是否存在
method_exists ( mixed $object , string $method_name ) : bool property_exists ( mixed $class , string $property ) : bool
示例 - 實現(xiàn)一個回調(diào)函數(shù),用戶可通過方法或者屬性來定義回調(diào)的 URL
trait RedirectsUsers { public function redirectPath() { if (method_exists($this, 'redirectTo')) { return $this->redirectTo(); } return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; } }
類關(guān)系判斷
相關(guān)函數(shù):
is_a — 對象屬于該類或該類的父類,返回 TRUE
is_subclass_of — 對象是該類的子類,返回 TRUE
is_a ( object $object , string $class_name [, bool $allow_string = FALSE ] ) : bool is_subclass_of ( object $object , string $class_name ) : bool
示例
interface A {} interface B {} class BaseFoo implements B {} class Foo extends BaseFoo implements A{} $foo = new Foo(); // 對象 is_a($foo, 'BaseFoo'); // true is_a($foo, 'Foo'); // true is_a($foo, 'A'); // true // 類 is_a('Foo', 'BaseFoo'); // false is_a('Foo', 'BaseFoo', true); // true, 傳入第三個參數(shù),代表允許使用類名而不是示例 is_subclass_of($foo, 'Foo'); // false is_subclass_of($foo, 'BaseFoo'); // true is_subclass_of($foo, 'B'); // true
實際情況中,更多的是使用操作符 instanceof
$foo instanceof Foo; // true $foo instanceof A; // true $foo instanceof B; // true
操作
相關(guān)函數(shù):
class_alias() - 為一個類創(chuàng)建別名 class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ) : bool
示例 - 類別名加載器,用于管理類的別名
class AliasLoader { private $aliases; public function __construct(array $aliases) { $this->aliases = $aliases; } public function load($alias) { if (isset($this->aliases[$alias])) { return class_alias($this->aliases[$alias], $alias); } } } class LongLongLongLongFoo {} $aliases = [ 'Foo' => 'LongLongLongLongFoo', 'Bar' => 'LongLongLongLongBar', ]; $loader = new AliasLoader($aliases); $loader->load('Foo'); $foo = new Foo(); var_dump($foo); // object(LongLongLongLongFoo)#3395
獲取
獲取全部
相關(guān)函數(shù):
get_declared_traits — 返回所有已定義的 traits 的數(shù)組
get_declared_interfaces — 返回一個數(shù)組包含所有已聲明的接口
get_declared_classes — 返回由已定義類的名字所組成的數(shù)組
這些函數(shù)在實際中很少需要用到
foreach (get_declared_classes() as $class) { $r = new \ReflectionClass($class); }
獲取類
相關(guān)函數(shù)
get_called_class — 后期靜態(tài)綁定類的名稱,在類外部使用返回 false
get_class — 返回對象的類名
get_parent_class — 返回對象或類的父類名
get_called_class ( void ) : array get_class ([ object $object = NULL ] ) : string get_parent_class ([ mixed $obj ] ) : string
示例 - 拋出異常時獲取異常的類
throw (new ModelNotFoundException)->setModel(get_called_class());
獲取類成員
相關(guān)函數(shù):
get_class_methods — 返回由類的方法名組成的數(shù)組
get_class_vars — 返回由類的默認(rèn)屬性組成的數(shù)組
get_object_vars — 返回由對象屬性組成的關(guān)聯(lián)數(shù)組
示例 - 獲取類中的所有訪問器屬性
class Foo { public function getFullNameAttribute() { } public function getTextAttribute() { } public static function getMutatorMethods() { preg_match_all('/(?<=^|;)get([^;]+?)Attribute(;|$)/', implode(';', get_class_methods(static::class)), $matches); return $matches[1]; } } Foo::getMutatorMethods() // [ // "FullName", // "Text", // ]
以上就是關(guān)于PHP函數(shù)庫之類與對象的詳解的簡略介紹,當(dāng)然詳細(xì)使用上面的不同還得要大家自己使用過才領(lǐng)會。如果想了解更多,歡迎關(guān)注億速云行業(yè)資訊頻道哦!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。