1. interface_exists、class_exists、method_exists和property_exists:
顾名思义,从以上几个函数的命名便可以猜出几分他们的功能。我想这也是我随着对PHP的深入学习而越来越喜欢这门编程语言的原因了吧。下面先给出他们的原型声明和简短说明,更多的还是直接看例子代码吧。
bool interface_exists (string $interface_name [, bool $autoload = true ]) 判断接口是否存在,第二个参数表示在查找时是否执行__autoload。
bool class_exists (string $class_name [, bool $autoload = true ]) 判断类是否存在,第二个参数表示在查找时是否执行__autoload。
bool method_exists (mixed $object , string $method_name) 判断指定类或者对象中是否含有指定的成员函数。
bool property_exists (mixed $class , string $property) 判断指定类或者对象中是否含有指定的成员变量。
代码如下 | 复制代码 |
} class AnotherTestClass {
//in class_exist_test.php, 下面测试代码中所需的类和接口位于another_test_class.php, print "The following case is tested before executing autoload.n"; if (!interface_exists('AnotherTestInterface',false)) { print "nThe following case is tested after executing autoload.n"; if (interface_exists('AnotherTestInterface',true)) { |
运行结果如下:
bogon:TestPhp$ php class_exist_test.php
The following case is tested before executing autoload.
This class doesn't exist if no autoload.
This interface doesn't exist if no autoload.
The following case is tested after executing autoload.
This class exists if autoload is set to true.
This interface exists if autoload is set to true.2. get_declared_classes和get_declared_interfaces:
分别返回当前可以访问的所有类和接口,这不仅包括自定义类和接口,也包括了PHP内置类和接口。他们的函数声明非常简单,没有参数,只是返回数组。见如下代码:
代码如下 | 复制代码 |
} class AnotherTestClass { print_r(get_declared_interfaces()); |
由于输出结果过长,而且这两个函数也比较简单,所以下面就不再给出输出结果了。
3. get_class_methods、get_class_vars和get_object_vars:
这三个函数有一个共同点,即只能获取作用域可见范围内的所有成员函数、成员变量或非静态成员变量。比如在类的内部调用,则所有成员函数或者变量都符合条件,而在类的外部,则只有共有的函数和变量可以返回。
array get_class_methods (mixed $class_name) 获取指定类中可访问的成员函数。
array get_class_vars (string $class_name) 获取指定类中可以访问的成员变量。
array get_object_vars (object $object) 获取可以访问的非静态成员变量。
代码如下 | 复制代码 |
class TestClass { private function privateFunction() { } $testObj = new TestClass(); print "nThe following is output out of TestClass.n";
The following is output out of TestClass. string get_class ([ object $object = NULL ]) www.111Cn.net获取参数对象的类名称。
class Derive extends Base { Base::test(); var_dump(get_class(new Base())); 运行结果如下: bogon:TestPhp$ php another_test_class.php |
5. get_parent_class、is_a和is_subclass_of:
这三个函数都是和类的继承相关,所以我把他们归到了一起。
string get_parent_class ([ mixed $object ]) 获取参数对象的父类,如果没有父类则返回false。
bool is_a (object $object, string $class_name) 判断第一个参数对象是否是$class_name类本身或是其父类的对象。
bool is_subclass_of (mixed $object, string $class_name) 判断第一个参数对象是否是$class_name的子类。
代码如下 | 复制代码 |
class Base { class Derive extends Base { var_dump(get_parent_class(new Derive())); var_dump(is_subclass_of(new Derive(),'Derive'));
|
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级