1. get_class — 傳回對象的類名
string get_class ([ object $obj ] )
傳回對象執行個體 obj 所屬類的名字。如果 obj 不是一個對象則傳回 FALSE。
Note: 在 PHP 擴充庫中定義的類傳回其原始定義的名字。在 PHP 4 中 get_class() 傳回使用者定義的類名的小寫形式,但是在 PHP 5 中将傳回類名定義時的名字,如同擴充庫中的類名一樣
//Note: 自 PHP 5 起,如果在對象的方法中調用則 obj 為可選項。
//Example#1 使用 get_class()
<?php
class foo {
function foo()
{
// implements some logic
}
function name()
{
echo "My name is " , get_class($this) , "/n";
}
}
// create an object
$bar = new foo();
// external call
echo "Its name is " , get_class($bar) , "/n";
// internal call
$bar->name();
?>
上例将輸出:
Its name is foo
My name is foo
2.get_class_methods — 傳回由類的方法名組成的數組
說明
array get_class_methods ( mixed $class_name )
傳回由 class_name 指定的類中定義的方法名所組成的數組。如果出錯,則傳回 NULL。
Example#1 get_class_methods() 示例
<?php
class myclass
{
// constructor
function myclass()
{
return(true);
}
// method 1
function myfunc1()
{
return(true);
}
// method 2
function myfunc2()
{
return(true);
}
}
$class_methods = get_class_methods('myclass');
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) { echo "$method_name/n";
}
?>
上例将輸出:
myclass
myfunc1
myfunc2
get_class_vars — 傳回由類的預設屬性組成的數組
說明
array get_class_vars ( string $class_name )
傳回由類的預設公有屬性組成的關聯數組,此數組的元素以 varname => value 的形式存在。
<?php
class myclass {
var $var1; // 此變量沒有預設值……
var $var2 = "xyz";
var $var3 = 100;
private $var4; // PHP 5
// constructor
function myclass() {
// change some properties
$this->var1 = "foo";
$this->var2 = "bar";
return true;
}
}
$my_class = new myclass();
$class_vars = get_class_vars(get_class($my_class));
foreach ($class_vars as $name => $value)
{
echo "$name : $value/n";
}
?>
上例輸出:
// 在 PHP 4.2.0 之前
var2 : xyz
var3 : 100
// 從 PHP 4.2.0 開始
var1 :
var2 : xyz
var3 : 100
class Memd extends Memcached {
/**
* 擷取Memcached内置屬性與函數
* @param int $flag default 1
*/
public function getMemcachedVar($flag = 1) {
$r = new ReflectionClass($this);
switch($flag) {
case 1:
return $r->getConstants();
case 2:
return $r->getMethods();
}
}
}
$cls = new Memd();
$info = $cls->getMemcachedVar(1);
print_r($info);
①Reflection類
class Reflection
{
public static mixed export(Reflector r [,bool return])
//導出一個類或方法的詳細資訊
public static array getModifierNames(int modifiers)
//取得修飾符的名字
}
②ReflectionException類
該類繼承标準類,沒特殊方法和屬性。
③ReflectionFunction類
class ReflectionFunction implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//導出該函數的詳細資訊
public string getName()
//取得函數名
public bool isInternal()
//測試是否為系統内部函數
public bool isUserDefined()
//測試是否為使用者自定義函數
public string getFileName()
//取得檔案名,包括路徑名
public int getStartLine()
//取得定義函數的起始行
public int getEndLine()
//取得定義函數的結束行
public string getDocComment()
//取得函數的注釋
public array getStaticVariables()
//取得靜态變量
public mixed invoke(mixed* args)
//調用該函數,通過參數清單傳參數
public mixed invokeArgs(array args)
//調用該函數,通過數組傳參數
public bool returnsReference()
//測試該函數是否傳回引用
public ReflectionParameter[] getParameters()
//取得該方法所需的參數,傳回值為對象數組
public int getNumberOfParameters()
//取得該方法所需的參數個數
public int getNumberOfRequiredParameters()
//取得該方法所需的參數個數
}
④ReflectionParameter類:
class ReflectionParameter implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//導出該參數的詳細資訊
public string getName()
//取得參數名
public bool isPassedByReference()
//測試該參數是否通過引用傳遞參數
public ReflectionClass getClass()
//若該參數為對象,傳回該對象的類名
public bool isArray()
//測試該參數是否為數組類型
public bool allowsNull()
//測試該參數是否允許為空
public bool isOptional()
//測試該參數是否為可選的,當有預設參數時可選
public bool isDefaultValueAvailable()
//測試該參數是否為預設參數
public mixed getDefaultValue()
//取得該參數的預設值
}
⑤ReflectionClass類:
class ReflectionClass implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//導出該類的詳細資訊
public string getName()
//取得類名或接口名
public bool isInternal()
//測試該類是否為系統内部類
public bool isUserDefined()
//測試該類是否為使用者自定義類
public bool isInstantiable()
//測試該類是否被執行個體化過
public bool hasConstant(string name)
//測試該類是否有特定的常量
public bool hasMethod(string name)
//測試該類是否有特定的方法
public bool hasProperty(string name)
//測試該類是否有特定的屬性
public string getFileName()
//取得定義該類的檔案名,包括路徑名
public int getStartLine()
//取得定義該類的開始行
public int getEndLine()
//取得定義該類的結束行
public string getDocComment()
//取得該類的注釋
public ReflectionMethod getConstructor()
//取得該類的構造函數資訊
public ReflectionMethod getMethod(string name)
//取得該類的某個特定的方法資訊
public ReflectionMethod[] getMethods()
//取得該類的所有的方法資訊
public ReflectionProperty getProperty(string name)
//取得某個特定的屬性資訊
public ReflectionProperty[] getProperties()
//取得該類的所有屬性資訊
public array getConstants()
//取得該類所有常量資訊
public mixed getConstant(string name)
//取得該類特定常量資訊
public ReflectionClass[] getInterfaces()
//取得接口類資訊
public bool isInterface()
//測試該類是否為接口
public bool isAbstract()
//測試該類是否為抽象類
public bool isFinal()
//測試該類是否聲明為final
public int getModifiers()
//取得該類的修飾符,傳回值類型可能是個資源類型
//通過Reflection::getModifierNames($class->getModifiers())進一步讀取
public bool isInstance(stdclass object)
//測試傳入的對象是否為該類的一個執行個體
public stdclass newInstance(mixed* args)
//建立該類執行個體
public ReflectionClass getParentClass()
//取得父類
public bool isSubclassOf(ReflectionClass class)
//測試傳入的類是否為該類的父類
public array getStaticProperties()
//取得該類的所有靜态屬性
public mixed getStaticPropertyValue(string name [, mixed default])
//取得該類的靜态屬性值,若private,則不可通路
public void setStaticPropertyValue(string name, mixed value)
//設定該類的靜态屬性值,若private,則不可通路,有悖封裝原則
public array getDefaultProperties()
//取得該類的屬性資訊,不含靜态屬性
public bool isIterateable()
public bool implementsInterface(string name)
//測試是否實作了某個特定接口
public ReflectionExtension getExtension()
public string getExtensionName()
}
⑥ReflectionMethod類:
class ReflectionMethod extends ReflectionFunction
{
public __construct(mixed class, string name)
public string __toString()
public static string export()
//導出該方法的資訊
public mixed invoke(stdclass object, mixed* args)
//調用該方法
public mixed invokeArgs(stdclass object, array args)
//調用該方法,傳多參數
public bool isFinal()
//測試該方法是否為final
public bool isAbstract()
//測試該方法是否為abstract
public bool isPublic()
//測試該方法是否為public
public bool isPrivate()
//測試該方法是否為private
public bool isProtected()
//測試該方法是否為protected
public bool isStatic()
//測試該方法是否為static
public bool isConstructor()
//測試該方法是否為構造函數
public bool isDestructor()
//測試該方法是否為析構函數
public int getModifiers()
//取得該方法的修飾符
public ReflectionClass getDeclaringClass()
//取得該方法所屬的類
// Inherited from ReflectionFunction
final private __clone()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public bool returnsReference()
public ReflectionParameter[] getParameters()
public int getNumberOfParameters()
public int getNumberOfRequiredParameters()
}
⑦ReflectionProperty類:
class ReflectionProperty implements Reflector
{
final private __clone()
public __construct(mixed class, string name)
public string __toString()
public static string export()
//導出該屬性的詳細資訊
public string getName()
//取得該屬性名
public bool isPublic()
//測試該屬性名是否為public
public bool isPrivate()
//測試該屬性名是否為private
public bool isProtected()
//測試該屬性名是否為protected
public bool isStatic()
//測試該屬性名是否為static
public bool isDefault()
public int getModifiers()
//取得修飾符
public mixed getValue(stdclass object)
//取得該屬性值
public void setValue(stdclass object, mixed value)
//設定該屬性值
public ReflectionClass getDeclaringClass()
//取得定義該屬性的類
public string getDocComment()
//取得該屬性的注釋
}
⑧ReflectionExtension類
class ReflectionExtension implements Reflector {
final private __clone()
public __construct(string name)
public string __toString()
public static string export()
//導出該擴充的所有資訊
public string getName()
//取得該擴充的名字
public string getVersion()
//取得該擴充的版本
public ReflectionFunction[] getFunctions()
//取得該擴充的所有函數
public array getConstants()
//取得該擴充的所有常量
public array getINIEntries()
//取得與該擴充相關的,在php.ini中的指令資訊
public ReflectionClass[] getClasses()
public array getClassNames()
}
- __CLASS__ 擷取目前類名(在此之前我用get_class - -!)
- __FUNCTION__ 目前函數名(confirm)
- __METHOD__ 目前方法名 (bankcard::confirm)