合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[速查手册:](https://www.w3cschool.cn/php/dict) [PHP - 开发者手册](https://cloud.tencent.com/developer/doc/1187) # **类常用的判断** 获取当前脚本之前所有的加载文件 ``` get_included_files() ``` 是否是CLI模式,服务器的系统 ``` define('IS_CLI', PHP_SAPI == 'cli' ? true : false); define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); ``` #### **获取某个文件夹下的文件:** ~~~ $files = glob($filePath . '*.php'); ~~~ #### **ClassName::class : 返回带命名空间的完整的类名** ~~~ namespace think; class Test{ } var_dump(Test::class); // "think\Test" ~~~ #### **self::ClassName 与 static::ClassName** 类内部获取类名 ~~~ class Parent { public static function getParent() { //self定义在父类就返回父类的名字,与实例化对象是子类还是父类无关 return self::class; } public static function getChild() { return static::class; } } class Son extends Parent { } echo Son::getParent(), PHP_EOL;//parent echo Son::getChild(), PHP_EOL;//Son ~~~ ### **is_subclass_of:判断一个对象是否为一个类的子类** ``` is_subclass_of('SonObject', 'ParentString') ``` 1.判断一个类是否存在 ``` class_exists($className) ``` 检查指定的 trait 是否存在**trait_exists** trait_exists(string $trait, bool $autoload = true): bool ``` trait World {} if ( trait_exists( 'World' ) ) {} echo Hello::World()->text('!!!'); // Hello World!!! ``` 2.判断方法是否存在类中存在 ``` method_exists($objectName,$methodName) ``` 3.判断属性是否存在类或者对象中 ``` property_exists($objectName|$className, $propertyName) ``` 判断一个接口是否存在(是否定义过); ``` interface_exists(“接口名”) ``` 4.获取对象的类名 ``` $objectName=new User(); get_class($objectName) ``` 5.获取对象对应类的父类 ``` get_parent_class($objectName) ``` **6.获取类中的方法,返回一个由方法名组成的数组** ``` get_class_methods($className|$objectName) ``` **7.获取对象中默认的属性(可访问非静态属性),返回由属性组成的数组** ``` get_object_vars($objectName) ``` ``` class foo { private $a; public $b = 1; public $c; private $d; static $e; public function test() { var_dump(get_object_vars($this)); } } $test = new foo; var_dump(get_object_vars($test)); array(2) { ["b"]=>int(1) ["c"]=>NULL } $test->test(); array(4) { ["a"]=>NULL ["b"]=>int(1) ["c"]=>NULL ["d"]=>NULL } ``` 获取类中默认的属性和值(中途赋值或者修改的值不能获取): ``` get_class_vars() //一般这么用: get_class_vars(get_class(类对象)); ``` **获取类对象方法、属性、常量列表** ``` $r = new ReflectionClass($serv); print_r($r->getConstants()); print_r($r->getProperties()); print_r($r->getMethods()); ``` 获取静态方法调用的类名 ``` class foo { static public function test() { var_dump(get_called_class()); } } class bar extends foo { } foo::test(); //string(3) "foo" bar::test();//string(3) "bar" ``` 判断是否实现了某个接口: ``` is_instanceof或class_implements() ``` 判断某个变量是否是一个对象 ``` is_object($obj) ``` 获取php所有的常量和他们的值(PHP 4 >= 4.1.0, PHP 5)(包含系统和自定义) ``` get_defined_constants([ bool $categorize = false ] ) ``` 获取php自定义的变量(除自定义的还有_GET、_POST、_FILES、_COOKIE) ``` get_defined_vars(void) ``` 获取所有已经定义的函数(PHP 4 >= 4.0.4, PHP 5) ~~~ get_defined_functions(void) ~~~ 获取所有可用的模块(PHP 4, PHP 5) ~~~ get_loaded_extensions(void) ~~~ 获取指定模块所有可用的函数。传入的参数(模块名称)必须是小写(PHP 4, PHP 5) ~~~ get_extension_funcs(string $module_name) ~~~ 获得“整个系统”所定义的类名,结果是一个数组,里面存储的是这些类的名称 (PHP 4, PHP 5) ~~~ get_declared_classes( void ) ~~~ 返回一个数组包含所有已声明的接口 ~~~ print_r(get_declared_interfaces()); //输出类似于: Array ( [0] => Traversable [1] => IteratorAggregate [2] => Iterator [3] => ArrayAccess [4] => reflector [5] => RecursiveIterator [6] => SeekableIterator ) ~~~ 返回所有已定义的 traits 的数组 ``` namespace Example; // Declare Trait trait FooTrait { } // Declare Abstract class abstract class FooAbstract { } // Declare class class Bar extends FooAbstract { use FooTrait; } // Get all traits declareds $array = get_declared_traits(); var_dump($array); /** * Result: * array(1) { * [0] => * string(23) "Example\FooTrait" * } */ ``` 返回指定对象的所有属性和值 ``` class A { public $public = 1; protected $protected = 2; private $private = 3; } class B extends A { private $private = 4; } $object = new B; $object->dynamic = 5; $object->{'6'} = 6; var_dump(get_mangled_object_vars($object)); array(6) { ["Bprivate"]=> int(4) ["public"]=> int(1) ["*protected"]=> int(2) ["Aprivate"]=> int(3) ["dynamic"]=> int(5) [6]=> int(6) } class AO extends ArrayObject { private $private = 1; } $arrayObject = new AO(['x' => 'y']); $arrayObject->dynamic = 2; var_dump(get_mangled_object_vars($arrayObject)); array(2) { ["AOprivate"]=> int(1) ["dynamic"]=> int(2) } ``` 检查对象是否属于一个给定的类型或子类型 同 **instanceof** ``` // 定义类 class WidgetFactory { var $oink = 'moo'; } // 创建新对象 $WF = new WidgetFactory(); if (is_a($WF, 'WidgetFactory')) { echo "yes, \$WF is still a WidgetFactory\n"; } //同 if ($WF instanceof WidgetFactory) { echo 'Yes, $WF is a WidgetFactory'; } ``` 为类创建别名**class_alias** ``` class Foo { } class_alias('Foo', 'Bar'); ``` 检测是否定义对应的枚举**enum_exists**(PHP 8 >= 8.1.0) enum_exists(string $enum, bool $autoload = true): bool ``` // 在使用之前检测枚举是否存在 if (enum_exists(Suit::class)) { $myclass = Suit::Hearts; } ```