3. get_class_methods、get_class_vars和get_object_vars:
这三个函数有一个共同点,即只能获取作用域可见范围内的所有成员函数、成员变量或非静态成员变量。比如在类的内部调用,则所有成员函数或者变量都符合条件,而在类的外部,则只有共有的函数和变量可以返回。
function output_array($functionName, $items) {
print "$functionName.....................\n";
foreach ($items as $key => $value) {
print '$key = '.$key. ' => $value = '.$value."\n";
}
}
class TestClass {
public $publicVar = 1;
private $privateVar = 2;
static private $staticPrivateVar = "hello";
static public $staticPublicVar;
private function privateFunction() {
}
function publicFunction() {
output_array("get_class_methods",get_class_methods(__CLASS__));
output_array('get_class_vars',get_class_vars(__CLASS__));
output_array('get_object_vars',get_object_vars($this));
}
}
$testObj = new TestClass();
print "The following is output within TestClass.\n";
$testObj->publicFunction();
print "\nThe following is output out of TestClass.\n";
output_array('get_class_methods',get_class_methods('TestClass'));
output_array('get_class_vars',get_class_vars('TestClass'));
output_array('get_object_vars',get_object_vars($testObj));
运行结果如下:
bogon:TestPhp liulei$ php class_exist_test.php
The following is output within TestClass.
get_class_methods.....................
$key = 0 => $value = privateFunction
$key = 1 => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value = 1
$key = privateVar => $value = 2
$key = staticPrivateVar => $value = hello
$key = staticPublicVar => $value =
get_object_vars.....................
$key = publicVar => $value = 1
$key = privateVar => $value = 2
The following is output out of TestClass.
get_class_methods.....................
$key = 0 => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value = 1
$key = staticPublicVar => $value =
get_object_vars.....................
$key = publicVar => $value = 1
4. get_called_class和get_class:
string get_class ([ object $object = NULL ]) 获取参数对象的类名称。
string get_called_class (void) 静态方法调用时当前的类名称。
class Base {
static public function test() {
var_dump(get_called_class());
}
}
class Derive extends Base {
}
Base::test();
Derive::test();
var_dump(get_class(new Base()));
var_dump(get_class(new Derive()));
运行结果如下:
bogon:TestPhp$ php another_test_class.php
string(4) "Base"
string(6) "Derive"
string(4) "Base"
string(6) "Derive"
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 {
static public function test() {
var_dump(get_called_class());
}
}
class Derive extends Base {
}
var_dump(get_parent_class(new Derive()));
var_dump(is_a(new Derive(),'Derive'));
var_dump(is_a(new Derive(),'Base'));
var_dump(is_a(new Base(),'Derive'));
var_dump(is_subclass_of(new Derive(),'Derive'));
var_dump(is_subclass_of(new Derive(),'Base'));
运行结果如下:
bogon:TestPhp$ php another_test_class.php
string(4) "Base"
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
http://www.bkjia.com/PHPjc/678024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/678024.htmlTechArticle1. interface_exists、class_exists、method_exists和property_exists: 顾名思义,从以上几个函数的命名便可以猜出几分他们的功能。我想这也是我随着对...