dongza1708 2014-11-20 09:43
浏览 76

找不到DOMDocument

I'm trying to code my own "HTML Generator" so i won't have to write HTML as string anymore, but the problem is that PHP doesn't recognize the DOMDocument class, it tries to load a Class with the name DOMDocument in the same namespace, which throws the error, I tried adding the backslah but no luck, here's my code :

<?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;
    }
}

the code of the action in the controller that uses this class :

<?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();

Here's the error message :

Fatal error: Class 'Services\HtmlGenerator\DOMDocument' not found in C:\xampp\htdocs\erp\services\htmlGenerator\Html.php on line 10

I'm using XAMPP 1.8.3 with PHP 5.5.15 on a Windows 7 machine.

I also want to mention that when I use $html = new \DOMDocument; in my controller it works just fine.

  • 写回答

3条回答 默认 最新

  • douwulu2576 2014-11-20 09:45
    关注

    Add use \DOMDocument; after the namespace line

    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致