溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

關于PHP函數庫之類與對象的詳解

發布時間:2020-04-29 10:57:42 來源:億速云 閱讀:333 作者:小新 欄目:編程語言

今天小編給大家分享的是關于PHP函數庫之類與對象的詳解,相信很多人都不太了解,為了讓大家更加了解PHP函數庫之類與對象,所以給大家總結了以下內容,一起往下看吧。一定會有所收獲的哦。

關于PHP函數庫之類與對象的詳解

廢棄

一些函數已經被廢棄或者移除,請不要使用它們

__autoload - 7.2 版本廢棄

call_user_method_array - 7.0 版本移除

call_user_method - 7.0 版本移除

判斷

類的存在性檢查

相關函數

class_exists - 判斷類是否存在

interface_exists - 判斷接口是否存在

trait_exists - 判斷 Trait 是否存在

第二個參數用來決定如果尚未加載,是否使用自動加載。

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

示例 - 廣泛的類存在性檢查函數

function common_class_exists(string $class): bool
{
    return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
}

類成員的存在性檢查

相關函數:

property_exists - 檢查屬性是否存在

method_exists — 檢查方法是否存在

method_exists ( mixed $object , string $method_name ) : bool
property_exists ( mixed $class , string $property ) : bool

示例 - 實現一個回調函數,用戶可通過方法或者屬性來定義回調的 URL

trait RedirectsUsers
{
    public function redirectPath()
    {
        if (method_exists($this, 'redirectTo')) {
            return $this->redirectTo();
        }
        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }
}

類關系判斷

相關函數:

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, 傳入第三個參數,代表允許使用類名而不是示例
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

操作

相關函數:

class_alias() - 為一個類創建別名
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

獲取

獲取全部

相關函數:

get_declared_traits — 返回所有已定義的 traits 的數組

get_declared_interfaces — 返回一個數組包含所有已聲明的接口

get_declared_classes — 返回由已定義類的名字所組成的數組

這些函數在實際中很少需要用到

foreach (get_declared_classes() as $class) {
    $r = new \ReflectionClass($class);
}

獲取類

相關函數

get_called_class — 后期靜態綁定類的名稱,在類外部使用返回 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());

獲取類成員

相關函數:

get_class_methods — 返回由類的方法名組成的數組

get_class_vars — 返回由類的默認屬性組成的數組

get_object_vars — 返回由對象屬性組成的關聯數組

示例 - 獲取類中的所有訪問器屬性

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",
// ]

以上就是關于PHP函數庫之類與對象的詳解的簡略介紹,當然詳細使用上面的不同還得要大家自己使用過才領會。如果想了解更多,歡迎關注億速云行業資訊頻道哦!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女