PHP函数库之类与对象详解-php教程

资源魔 31 0

d4d7df4559228110009cdce365a51ee.png

烧毁

一些函数曾经被烧毁或许移除了,请没有要应用它们

__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 co妹妹on_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函数库之类与工具详解的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!