【问题标题】:Declaring variable as PHP class将变量声明为 PHP 类
【发布时间】:2010-06-12 03:16:02
【问题描述】:

我有一个名为 $theclass 的变量,它是一个字符串“Home_class”。如何从该字符串中定义一个类?我需要这样做的原因是变量会发生变化,我希望能够声明变量等于的类。

【问题讨论】:

标签: php class


【解决方案1】:

使用new 关键字。

$theclass = 'My_class';
$x = new $theclass(); // $x is now an instance of My_class

【讨论】:

  • 效果很好,不过我必须等待 11 分钟才能将答案标记为正确。谢谢。
猜你喜欢
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 2018-08-25
  • 2012-06-26
  • 2010-09-28
  • 2013-09-06
  • 1970-01-01
相关资源
最近更新 更多