php php中的动态类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7131295/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:06:13  来源:igfitidea点击:

dynamic class names in php

phpoopwordpress

提问by andrew mclagan

I have a base class called fieldand classes that extend this class such as text, select, radio, checkbox, date, time, number, etc.

我有一个基类field和扩展这个类的类,例如text, select, radio, checkbox, date, time, number, 等。

Classes that extend fieldclass are dynamically called in a directory recursively using include_once(). I do this so that I ( and others) can easily add a new field type only by adding a single file

扩展field类的类在目录中使用include_once(). 我这样做是为了我(和其他人)只需添加一个文件就可以轻松添加新的字段类型

What I want to know: Is there a way to substantiate a new object from one of these dynamically included extending classes from a variable name?

我想知道的是:有没有办法从这些动态包含的扩展类中的一个实例化一个新对象?

e.g. a class with the name checkbox:

例如一个名为的类checkbox

$field_type = 'checkbox';

$field = new {$field_type}();

Maybe this would work? but it does not?

也许这会奏效?但它没有?

$field_type = 'checkbox';

$field = new $$field_type();

回答by sg3s

This should work to instantiate a class with a string variable value:

这应该可以用字符串变量值实例化一个类:

$type = 'Checkbox'; 
$field = new $type();
echo get_class($field); // Output: Checkbox

So your code should work I'd imagine. What is your question again?

所以我想你的代码应该可以工作。你又问什么?

If you want to make a class that includes all extended classes then that is not possible. That's not how classes work in PHP.

如果您想创建一个包含所有扩展类的类,那么这是不可能的。这不是类在 PHP 中的工作方式。

回答by Sam_Benne

If you are using a namespace you will need to add it even if you are within the namespace.

如果您正在使用命名空间,即使您在命名空间内,也需要添加它。

namespace Foo;

$my_var = '\Foo\Bar';
new $my_var;

Otherwise it will not be able to get the class.

否则将无法获得该类。

回答by genesis

just

只是

$type = 'checkbox';
$filed = new $type();

is required. you do not need to add brackets

是必须的。你不需要添加括号

回答by zstate

Spent some time figuring this out. From PHP documentation Namespaces and dynamic language features:

花了一些时间弄清楚这一点。来自 PHP 文档命名空间和动态语言功能

Note that because there is no difference between a qualified and a fully qualified Name inside a dynamic class name, function name, or constant name, the leading backslash is not necessary.

请注意,由于动态类名、函数名或常量名中的限定名和完全限定名之间没有区别,因此不需要前导反斜杠。

namespace namespacename;
class classname
{
    function __construct()
    {
        echo __METHOD__,"\n";
    }
}
function funcname()
{
    echo __FUNCTION__,"\n";
}
const constname = "namespaced";

/* note that if using double quotes, "\namespacename\classname" must be used */
$a = '\namespacename\classname';
$obj = new $a; // prints namespacename\classname::__construct
$a = 'namespacename\classname';
$obj = new $a; // also prints namespacename\classname::__construct

$b = 'namespacename\funcname';
$b(); // prints namespacename\funcname
$b = '\namespacename\funcname';
$b(); // also prints namespacename\funcname

echo constant('\namespacename\constname'), "\n"; // prints namespaced
echo constant('namespacename\constname'), "\n"; // also prints namespaced

回答by Mchl

This should be enough:

这应该足够了:

$field_type = 'checkbox';
$field = new $field_type();

Code I tested it with in PHP 5.3

我在 PHP 5.3 中测试过的代码

$c = 'stdClass';

$a = new $c();

var_dump($a);

>> object(stdClass)#1 (0) {
}

回答by RiaD

$field_type = 'checkbox';
$field = new $field_type;

If you need arguments:

如果您需要参数:

$field_type = 'checkbox';
$field = new $field_type(5,7,$user);

回答by shesek

You can also use reflection, $class = new ReflectionClass($class_name); $instance = $class->newInstance(arg1, arg2, ...);

您还可以使用反射, $class = new ReflectionClass($class_name); $instance = $class->newInstance(arg1, arg2, ...);