PHP Conference Japan 2024

ReflectionClass::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionClass::__construct建構 ReflectionClass

說明

public ReflectionClass::__construct(物件|字串 $objectOrClass)

建構新的 ReflectionClass 物件。

參數

objectOrClass

包含要反射的類別名稱的 字串物件

錯誤/例外

如果要反射的類別不存在,則擲出 ReflectionException

範例

範例 #1 ReflectionClass 的基本用法

<?php
$reflection
= new ReflectionClass('Exception');
echo
$reflection;
?>

以上範例的輸出結果類似如下:

Class [ <internal:Core> class Exception implements Stringable, Throwable ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [7] {
    Property [ protected $message = '' ]
    Property [ private string $string = '' ]
    Property [ protected $code = 0 ]
    Property [ protected string $file = '' ]
    Property [ protected int $line = 0 ]
    Property [ private array $trace = [] ]
    Property [ private ?Throwable $previous = NULL ]
  }

  - Methods [11] {
    Method [ <internal:Core> private method __clone ] {

      - Parameters [0] {
      }
      - Return [ void ]
    }

    Method [ <internal:Core, ctor> public method __construct ] {

      - Parameters [3] {
        Parameter #0 [ <optional> string $message = "" ]
        Parameter #1 [ <optional> int $code = 0 ]
        Parameter #2 [ <optional> ?Throwable $previous = null ]
      }
    }

    Method [ <internal:Core> public method __wakeup ] {

      - Parameters [0] {
      }
      - Tentative return [ void ]
    }

    Method [ <internal:Core, prototype Throwable> final public method getMessage ] {

      - Parameters [0] {
      }
      - Return [ string ]
    }

    Method [ <internal:Core, prototype Throwable> final public method getCode ] {

      - Parameters [0] {
      }
    }

    Method [ <internal:Core, prototype Throwable> final public method getFile ] {

      - Parameters [0] {
      }
      - Return [ string ]
    }

    Method [ <internal:Core, prototype Throwable> final public method getLine ] {

      - Parameters [0] {
      }
      - Return [ int ]
    }

    Method [ <internal:Core, prototype Throwable> final public method getTrace ] {

      - Parameters [0] {
      }
      - Return [ array ]
    }

    Method [ <internal:Core, prototype Throwable> final public method getPrevious ] {

      - Parameters [0] {
      }
      - Return [ ?Throwable ]
    }

    Method [ <internal:Core, prototype Throwable> final public method getTraceAsString ] {

      - Parameters [0] {
      }
      - Return [ string ]
    }

    Method [ <internal:Core, prototype Stringable> public method __toString ] {

      - Parameters [0] {
      }
      - Return [ string ]
    }
  }
}

參見

新增註解

使用者貢獻的註解 5 則註解

danbettles at yahoo dot co dot uk
9 年前
在 PHP 5.3 中要反射命名空間中的類別,您必須指定類別的完整名稱,即使您已使用「use」語句為包含的命名空間建立了別名。

因此,不要這樣寫:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('Core\Singleton');
?>

而應該這樣寫:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('App\Core\Singleton');
?>
me [at] klay [dot] me
11 年前
使用範例

public static function getClassData($class)
{
// 嘗試建立一個新的 ReflectionClass 類別物件
$class = new ReflectionClass($class);

$details = sprintf('%s - %s%s%s%s%s%s%s%s',
$class->getName(),
$class->isInternal() ? '內部類別,' : '使用者定義類別,',
$class->isTrait() ? ' 是 trait,' : '',
$class->isInterface() ? ' 是介面,' : '',
$class->isAbstract() ? ' 是抽象類別,' : '',
$class->isFinal() ? ' 是最終類別,' : '',
$class->isCloneable() ? ' 可複製,' : '',
$class->isInstantiable() ? ' 可實例化,' : '',
$class->isIterateable() ? ' 可迭代,' : ''
);

return '<pre class="debug">' . rtrim($details, ',') . '</pre>';
}
gafisher at griasolutions dot com
12 年前
在 Windows Vista(我知道,我知道)上執行以下程式碼,PHP 5.3.9,當所需的類別無法實例化時,ReflectionClass 建構子實際上會拋出 ReflectionException

<?php
try {
$ReflectedClass = new ReflectionClass('NonExist');
} catch (
LogicException $Exception) {
die(
'不會執行到這裡...');
} catch (
ReflectionException $Exception) {
die(
'您的類別不存在!');
}
?>
ivo at jansch dot nl
14 年前
知道您可以使用 ReflectionClass 來檢查介面是非常有用的,即使介面不是類別。範例:

<?php

介面 Edible
{
public function
eat();
}

$refl = new ReflectionClass("Edible");
$methods = $refl->getMethods();
?>

【由 danbrown AT php DOT net 編輯 - 包含 (dbl AT bnet DOT com) 於 2010 年 8 月 18 日提供的錯誤修正,訊息如下:「必須移除底線才能使其正常運作 ( new Reflection_Class -> new ReflectionClass )」】
cspray at gmail dot com
13 年前
如果將字串傳遞到建構函式中,且由於某些原因無法實例化該類別,則會拋出 SPL LogicException,知道這點很有用。

此程式碼在 Mac OS X 10.6.7、AMP、PHP 5.3+ 上執行。

<?php

// index.php
try {
$ReflectedClass = new ReflectionClass('NonExist');
} catch (
LogicException $logicDuh) {
print_r($logicDuh);
}

?>

將返回一個充滿錯誤相關有用資訊的深度巢狀陣列。
To Top