<?php //in another_test_class.php
interface AnotherTestInterface {
}
class AnotherTestClass {
public static function printMe() {
print "This is Test2::printSelf.\n";
}
public function doSomething() {
print "This is Test2::doSomething.\n";
}
public function doSomethingWithArgs($arg1, $arg2) {
print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").\n";
}
}
<?php //in class_exist_test.php, 下面测试代码中所需的类和接口位于another_test_class.php,
//由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。
//这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。
function __autoload($classname) {
$nomilizedClassname = strtolower(preg_replace('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',$classname));
require strtolower($nomilizedClassname).".php";
}
//spl_autoload_register(function($classname) {
// $nomilizedClassname = strtolower(preg_replace('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',$classname));
// require strtolower($nomilizedClassname).".php";
//});
print "The following case is tested before executing autoload.\n";
if (!class_exists('AnotherTestClass',false)) {
print "This class doesn't exist if no autoload.\n";
}
if (!interface_exists('AnotherTestInterface',false)) {
print "This interface doesn't exist if no autoload.\n";
}
print "\nThe following case is tested after executing autoload.\n";
if (class_exists('AnotherTestClass',true)) {
print "This class exists if autoload is set to true.\n";
}
if (interface_exists('AnotherTestInterface',true)) {
print "This interface exists if autoload is set to true.\n";
}
运行结果如下:
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内置类和接口。他们的函数声明非常简单,没有参数,只是返回数组。见如下代码:
<?php
interface AnotherTestInterface {
}
class AnotherTestClass {
public static function printMe() {
print "This is Test2::printSelf.\n";
}
}
print_r(get_declared_interfaces());
print_r(get_declared_classes());
由于输出结果过长,而且这两个函数也比较简单,所以下面就不再给出输出结果了。 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) 获取可以访问的非静态成员变量。
<?php
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));
运行结果如下:
<?php
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()));
运行结果如下: