我希望 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; } }
?>