interface_exists() / class_exists()<?php
bool interface_exists(string $interface_name, bool $autoload = true)
bool class_exists(string $class_name, bool $autoload = true)
$autoload = true(預設):觸發自動載入器嘗試載入類別/介面$autoload = false:僅檢查已載入的符號表,不觸發自動載入method_exists() / property_exists()<?php
bool method_exists(mixed $object_or_class, string $method_name)
bool property_exists(mixed $class_or_object, string $property_name)
property_exists 甚至能檢測未初始化的屬性(isset() 做不到)<?php
// 定義檔:AnotherTest.php
interface AnotherTestInterface {}
class AnotherTestClass {
public static function printMe() { echo "static\n"; }
public function doSomething() { echo "instance\n"; }
public function doSomethingWithArgs($a, $b) { echo "$a, $b\n"; }
}
// 測試檔
spl_autoload_register(function($class) {
$file = strtolower(preg_replace('/([A-Z])/', '_$1', $class)) . '.php';
if (file_exists($file)) require $file;
});
echo "=== 未觸發 autoload ===\n";
var_dump(class_exists('AnotherTestClass', false)); // false
var_dump(interface_exists('AnotherTestInterface', false)); // false
echo "=== 觸發 autoload ===\n";
var_dump(class_exists('AnotherTestClass', true)); // true
var_dump(interface_exists('AnotherTestInterface', true)); // true
?>
<?php
// 返回當前進程已載入的所有類別/介面名稱陣列(含內建)
$classes = get_declared_classes();
$interfaces = get_declared_interfaces();
echo "類別數量: " . count($classes) . "\n"; // 通常 100+
echo "介面數量: " . count($interfaces) . "\n"; // 通常 20+
// 檢查特定類別是否已載入
in_array('PDO', get_declared_classes()); // true
?>
| 函數 | 參數 | 返回 | 可見範圍 |
|---|---|---|---|
get_class_methods() | 類別名/物件 | 方法名陣列 | 呼叫處可見的方法 |
get_class_vars() | 類別名 | 屬性名=>預設值陣列 | 呼叫處可見的屬性 |
get_object_vars() | 物件實例 | 屬性名=>當前值陣列 | 物件可見的非靜態屬性 |
<?php
class TestClass {
public $publicVar = 1;
private $privateVar = 2;
static private $staticPrivate = 'hello';
static public $staticPublic;
private function privateMethod() {}
public function publicMethod() {}
public function inspectInternal() {
echo "=== 類別內部 ===\n";
print_r(get_class_methods(__CLASS__)); // 含 privateMethod
print_r(get_class_vars(__CLASS__)); // 含所有屬性
print_r(get_object_vars($this)); // 含 privateVar
}
}
$obj = new TestClass();
$obj->inspectInternal();
echo "\n=== 類別外部 ===\n";
print_r(get_class_methods('TestClass')); // 僅 publicMethod
print_r(get_class_vars('TestClass')); // 僅 publicVar, staticPublic
print_r(get_object_vars($obj)); // 僅 publicVar
?>
get_class() vs get_called_class()<?php
class Base {
public static function test() {
var_dump(get_called_class()); // 實際調用的類別
}
}
class Derived extends Base {}
Base::test(); // string(4) "Base"
Derived::test(); // string(6) "Derived"
var_dump(get_class(new Base())); // "Base"
var_dump(get_class(new Derived())); // "Derived"
?>
get_class($obj):返回物件實際類別名get_called_class():靜態方法中返回被調用的類別名(LSB 關鍵)| 函數 | 用途 | 返回 |
|---|---|---|
get_parent_class($obj|$class) | 取得父類別名 | string|false |
is_a($obj, $class) | $obj 是否為 $class 或其子類實例 | bool |
is_subclass_of($obj|$class, $parent) | 是否為 $parent 的子類 | bool |
<?php
class Base {}
class Derived extends Base {}
var_dump(get_parent_class(new Derived())); // "Base"
var_dump(is_a(new Derived(), 'Derived')); // true
var_dump(is_a(new Derived(), 'Base')); // true
var_dump(is_a(new Base(), 'Derived')); // false
var_dump(is_subclass_of(new Derived(), 'Derived')); // false(自身不算子類)
var_dump(is_subclass_of(new Derived(), 'Base')); // true
?>
上述函數為過程式風格,現代 PHP 建議使用 ReflectionClass 等反射類別,功能更強、型別安全:
<?php
$ref = new ReflectionClass('TestClass');
// 存在性
$ref->hasMethod('publicMethod'); // true
$ref->hasProperty('publicVar'); // true
// 詳細資訊
$method = $ref->getMethod('publicMethod');
$method->isPublic(); // true
$method->isStatic(); // false
$method->getParameters(); // 參數列表
$method->getReturnType(); // 返回型別 (PHP 7+)
$prop = $ref->getProperty('privateVar');
$prop->setAccessible(true); // 突破可見性
$prop->getValue($obj); // 讀取私有屬性值
// 繼承鏈
$ref->getParentClass(); // ReflectionClass|false
$ref->getInterfaces(); // ReflectionClass[]
$ref->implementsInterface('Traversable'); // bool
// 實例化
$instance = $ref->newInstanceArgs([$arg1, $arg2]);
?>
class_exists + is_subclass_of 驗證插件類別method_exists($handler, 'handle') 檢查再呼叫property_exists($dto, $field) 安全賦值ReflectionClass,支援屬性提升、唯讀屬性、型別提示分析ReflectionClassPHP 提供豐富的執行期型別檢測工具:class_exists/interface_exists 處理載入狀態,method_exists/property_exists 處理成員存在性,get_class/get_called_class/is_a 處理繼承關係。現代開發應以 Reflection API 為主,舊函數為輔,構建靈活、安全的動態系統。