我正在尝试编写自己的"HTML Generator“,这样我就不必再编写字符串形式的超文本标记语言了,但问题是PHP不能识别DOMDocument类,它试图在相同的命名空间中加载一个名称为DOMDocument的类,这会抛出错误,我尝试添加backslah,但没有成功,这是我的代码:
<?php
namespace Services\HtmlGenerator;
use \DOMDocument;
/**
* Services\HtmlGenerator\Html
*/
class Html extends DOMDocument
{
function __construct(){
parent::__construct('1.0','iso-8859-1' );
$this->formatOutput = true;
}
public function createInput($value, $name, $class = null)
{
$input = $this->createElement('input');
$input->setAttribute('value', $value);
$input->setAttribute('name', $name);
$input->setAttribute('class', $class);
return $input;
}
}使用此类的控制器中的操作代码:
<?php
namespace ModuleX\RemoteControllers;
use Services\HtmlGenerator\Html;
//...
class RemoteXController extends RemoteController
{
//...
public function action()
{
$html = new Html;
$elem = $html->createInput('test', 'test', 'test');
$html->appendChild($elem);
return $html->saveHTML();以下是错误消息:
Fatal error: Class 'Services\HtmlGenerator\DOMDocument' not found in C:\xampp\htdocs\erp\services\htmlGenerator\Html.php on line 10我在Windows7机器上使用XAMPP 1.8.3 with PHP 5.5.15。
我还想提一下,当我在我的控制器中使用$html = new \DOMDocument;时,它工作得很好。
发布于 2014-11-20 17:45:45
在名称空间行后添加use \DOMDocument;
发布于 2014-11-20 19:34:13
从另一个命名空间扩展类时,需要对extends语句使用完全限定的名称。例如:
<?php
namespace Services\HtmlGenerator;
class Html extends \DOMDocument
{
...
}注意extends语句中的前导反斜杠
发布于 2015-07-01 00:01:01
这通常是由于没有为PHP安装正确的模块造成的。如果您的服务器运行的是支持YUM的Linux发行版(如CentOS),则可以使用如下命令进行安装:
yum install php-xml.x86_64
然后只需重新启动apache (例如:/etc/init.d/httpd restart),它就应该可以运行了。
https://stackoverflow.com/questions/27036279
复制相似问题