PHP 函数库精讲之类与对象
发布时间:2020-12-13 21:03:30 所属栏目:PHP教程 来源:网络整理
导读:废弃 一些函数已经被废弃或者移除,请不要使用它们 __autoload ?- 7.2 版本废弃 call_user_method_array ?- 7.0 版本移除 call_user_method ?- 7.0 版本移除 判断 ? 类的存在性检查 相关函数 class_exists ?- 判断类是否存在 interface_exists ?- 判断接口是
废弃一些函数已经被废弃或者移除,请不要使用它们
判断? 类的存在性检查相关函数
第二个参数用来决定如果尚未加载,是否使用自动加载。 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); }
类成员的存在性检查相关函数:
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 ( 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
实际情况中,更多的是使用操作符? $foo instanceof Foo; // true $foo instanceof A; // true $foo instanceof B; // true
操作相关函数:
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
获取? 获取全部相关函数:
这些函数在实际中很少需要用到 foreach (get_declared_classes() as $class) { $r = new ReflectionClass($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());
获取类成员相关函数:
示例 - 获取类中的所有访问器属性 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",// ]
? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |