首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到DOMDocument

找不到DOMDocument
EN

Stack Overflow用户
提问于 2014-11-20 17:43:52
回答 3查看 2.9K关注 0票数 0

我正在尝试编写自己的"HTML Generator“,这样我就不必再编写字符串形式的超文本标记语言了,但问题是PHP不能识别DOMDocument类,它试图在相同的命名空间中加载一个名称为DOMDocument的类,这会抛出错误,我尝试添加backslah,但没有成功,这是我的代码:

代码语言:javascript
复制
<?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;
    }
}

使用此类的控制器中的操作代码:

代码语言:javascript
复制
<?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();

以下是错误消息:

代码语言:javascript
复制
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;时,它工作得很好。

EN

回答 3

Stack Overflow用户

发布于 2014-11-20 17:45:45

在名称空间行后添加use \DOMDocument;

票数 1
EN

Stack Overflow用户

发布于 2014-11-20 19:34:13

从另一个命名空间扩展类时,需要对extends语句使用完全限定的名称。例如:

代码语言:javascript
复制
<?php

namespace Services\HtmlGenerator;

class Html extends \DOMDocument
{
...
}

注意extends语句中的前导反斜杠

票数 1
EN

Stack Overflow用户

发布于 2015-07-01 00:01:01

这通常是由于没有为PHP安装正确的模块造成的。如果您的服务器运行的是支持YUM的Linux发行版(如CentOS),则可以使用如下命令进行安装:

yum install php-xml.x86_64

然后只需重新启动apache (例如:/etc/init.d/httpd restart),它就应该可以运行了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27036279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档