简单HTML Dom的两个实例


Two Instances of Simple HTML Dom

我目前正在使用php-include将两个文件源到一个网页上。

这两个不同的文件表示1.php和2.php有两个使用simplehtmldom的不同实例。

问题是2.php包含在网页上时,在其位置显示以下错误。

致命错误:无法在第85行的/home/nothsho/public_html/w/pages/simple_html_dom.php中重新声明file_get_html()

编辑:

进一步澄清。

master.php(包括1.php和2.php)

1.php和2.php都使用simple_html_dom.php

使用这种结构,尽管您可以不包括这两个文件,也不将内容移动到函数中,并将URL作为参数传递

master.php

<?php
include 'simple_html_dom.php';
include '1.php';
include '2.php';

1.php

<?php
$html = file_get_html('http://example.domain.com');
//do your job
//destroy the object at the end
$html->clear(); 
unset($html);

2.php

<?php
$html = file_get_html('http://anotherexample.domain.com');
//do your job
//destroy the object at the end
$html->clear(); 
unset($html);

master.php使用一个函数而不是包含2个其他文件

<?php
include 'simple_html_dom.php';
function doMyJob($url){
    $html = file_get_html($url);
    //do the actual job
    $result = $html->plaintext;
    $html->clear();
    unset($html);
    return $result;
}
//call the function
print doMyJob("http://example.com");
# FileA: simple_html_dom.php
if(!class_exists('simple_html_dom')){
    class simple_html_dom{
        // ...
    }
}
# FileB:include/require 'prefix_path/'.'simple_html_dom.php'