使用字符串创建 PHP 类实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4578335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating PHP class instance with a string
提问by Joel
I have two classes, class ClassOne { }
and class ClassTwo {}
. I am getting a string which can be either "One"
or "Two"
.
我有两个班级,class ClassOne { }
和class ClassTwo {}
. 我得到一个字符串,它可以是"One"
或"Two"
。
Instead of using a long switch
statement such as:
而不是使用长switch
语句,例如:
switch ($str) {
case "One":
return new ClassOne();
case "Two":
return new ClassTwo();
}
Is there a way I can create an instance using a string, i.e. new Class("Class" . $str);
?
有没有办法可以使用字符串创建实例,即new Class("Class" . $str);
?
回答by Bob Fanger
Yes, you can!
是的你可以!
$str = 'One';
$class = 'Class'.$str;
$object = new $class();
When using namespaces, supply the fully qualified name:
使用命名空间时,请提供完全限定名称:
$class = '\Foo\Bar\MyClass';
$instance = new $class();
Other cool stuff you can do in php are:
Variable variables:
你可以在 php 中做的其他很酷的事情是:
变量变量:
$personCount = 123;
$varname = 'personCount';
echo $$varname; // echo's 123
And variable functions & methods.
以及可变函数和方法。
$func = 'my_function';
$func('param1'); // calls my_function('param1');
$method = 'doStuff';
$object = new MyClass();
$object->$method(); // calls the MyClass->doStuff() method.
回答by John Parker
You can simply use the following syntax to create a new class (this is handy if you're creating a factory):
您可以简单地使用以下语法来创建一个新类(如果您正在创建工厂,这很方便):
$className = $whatever;
$object = new $className;
As an (exceptionally crude) example factory method:
作为(异常粗糙的)示例工厂方法:
public function &factory($className) {
require_once($className . '.php');
if(class_exists($className)) return new $className;
die('Cannot create new "' . $className . '" class - includes not found or class unavailable.');
}
回答by Freddie
have a look at example 3 from http://www.php.net/manual/en/language.oop5.basic.php
从http://www.php.net/manual/en/language.oop5.basic.php查看示例 3
$className = 'Foo';
$instance = new $className(); // Foo()
回答by Remade
Lets say ClassOne
is defined as:
让我们说ClassOne
定义为:
public class ClassOne
{
protected $arg1;
protected $arg2;
//Contructor
public function __construct($arg1, $arg2)
{
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
public function echoArgOne
{
echo $this->arg1;
}
}
Using PHP Reflection;
使用 PHP 反射;
$str = "One";
$className = "Class".$str;
$class = new \ReflectionClass($className);
Create a new Instance:
创建一个新实例:
$instance = $class->newInstanceArgs(["Banana", "Apple")]);
Call a method:
调用一个方法:
$instance->echoArgOne();
//prints "Banana"
Use a variable as a method:
使用变量作为方法:
$method = "echoArgOne";
$instance->$method();
//prints "Banana"
Using Reflection instead of just using the raw string to create an object gives you better control over your object and easier testability (PHPUnit relies heavily on Reflection)
使用反射而不是仅使用原始字符串来创建对象可以让您更好地控制对象并更容易测试(PHPUnit 严重依赖反射)