PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP函数库之类与对象详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP函数库之类与对象详解

废弃

一些函数已经被废弃或者移除,请不要使用它们

__autoload - 7.2 版本废弃

call_user_R_715_11845@ethod_array - 7.0 版本移除

call_user_R_715_11845@ethod - 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 - 检查属性是否存在

@H_505_9@method_exists — 检查方法是否存在

@H_676_39@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函数库之类与对象详解全部内容,希望文章能够帮你解决PHP函数库之类与对象详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: