[英]DOMDocument not found
我正在嘗試編寫自己的“ HTML生成器”,這樣就不必再將HTML編寫為字符串了,但是問題是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
我在Windows 7計算機上使用XAMPP 1.8.3 with PHP 5.5.15
。
我還想提及一下,當我使用$html = new \\DOMDocument;
在我的控制器中工作正常。
這通常是由於沒有為PHP安裝正確的模塊引起的。 如果您的服務器正在運行支持YUM的Linux發行版(例如CentOS),則可以使用以下命令進行安裝:
yum install php-xml.x86_64
然后只需重新啟動apache(例如: /etc/init.d/httpd restart
),它就可以開始使用了。
添加use \\DOMDocument;
在名稱空間行之后
當從另一個名稱空間擴展一個類時,您需要為extends
語句使用一個完全限定的名稱。 例如:
<?php
namespace Services\HtmlGenerator;
class Html extends \DOMDocument
{
...
}
注意extends
語句中的前導反斜杠
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.