我希望PHP通过命名空间内的间接变量引用来构造一个对象。它就像:
$ArticleObjectIdentifier = 'qmdArticle'excursions_list_item';
$result = new $ArticleObjectIdentifier($parent_obj,$r);
其中 qmdArticle 是使用的命名空间,excursions_list_item是类名 - 通常不是硬编码,而是从数据库中读取。
使用上述内容时,我收到以下错误:
Class 'qmdArticle''excursions_list_item' not found in /media/work/www/mytestarea/control.php on line 1916 ...
索引.php
<?php
namespace hy_soft'qimanfaya'testarea'main;
use hy_soft'qimanfaya'testarea'articles as article;
include_once('article.php');
$ArticleLoader = 'article'excursions_list_item';
$article = new $ArticleLoader();
$article->showcontent();
?>
》.php
<?php namespace hy_soft'qimanfaya'testarea'articles
class excursions_list_item { private $content; function
__construct() {
$this->content = 'This is the article body';
// parent::__construct($parent,$dbrBaseRec);
}
public function showcontent() { echo $this->content; } }
?>
我终于找到了一个类似的例子,但花了一段时间才真正得到它:
实际的技巧是使用双引号:>>"<<和双斜杠>>''<<并且它不适用于创建的别名,例如
use hy_soft'qimanfaya'testarea'articles as article;
必须使用完全限定的类名 (FQCN(
$ArticleLoader = "''hy_soft''qimanfaya''testarea'articles''excursions_list_item";
我仍然会欣赏如何使用别名的任何建议。谢谢。
工作示例:文章.php
<?php
namespace hy_soft'qimanfaya'testarea'articles;
class excursions_list_item
{
private $content;
function __construct()
{
$this->content = 'This is the article body';
// parent::__construct($parent,$dbrBaseRec);
}
public function showcontent()
{
echo $this->content;
}
}
?>
索引.php
<?php
namespace hy_soft'qimanfaya'testarea'main;
use hy_soft'qimanfaya'testarea'articles as article;
include_once('article.php');
$ArticleLoader = "''hy_soft''qimanfaya''testarea'articles''excursions_list_item";
//$ArticleLoader = "''article''excursions_list_item"; doesn't work
$article = new $ArticleLoader();
$article->showcontent();
?>