簡體   English   中英

這三種動態實例化對象的方式不同嗎?

[英]Differences in these 3 way to instantiate an object dynamically?

出於好奇,使用以下一種方法在PHP中創建實例有什么區別(如果有,例如性能)?

class MyClass { }

// Direct
$name     = 'MyClass';
$instance = new $name;

// Using ReflectionClass
$reflector = new ReflectionClass('MyClass');
$instance  = $reflector->newInstance();

// Really don't know if it's going to work
$instance = call_user_func(array('MyClass', '__construct'));

直接是“正常方式”

如果您的程序必須即時找出類等,則將使用ReflectionClass進行操作-在大多數情況下無需這樣做。 通常,這會占用更多的資源,並且速度較慢(也許不是很明顯)

不確定第三種方法-屬於KISS原則-自從“直接”工作以來,我從來沒有遇到過如此曲折的情況,甚至無法提出第三種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM