无法在一个类中加载simple-html-dom类两次

时间:2013-10-23 11:06:59

标签: php simple-html-dom

当我尝试在dom对象中加载secont时间的文件时(我清楚它),dom变量为空:( 我如何在班上两次使用simple-html-dom类?

<?php
require_once 'simple_html_dom.php';
class RO{
    public $dom;
    public $dom2;
    public function __construct(){
        $this->dom = new simple_html_dom();
        $this->dom2 = new simple_html_dom();
    }
    function GetDatumInUroPodatkovInTabKrajev($lang){
        $this->dom->load_file("http://www...diferentadresthenbelow...html");
        $tabelaTempInKrajev = $this->dom->find('table[@class="online"]');

        //some code...

        //some code...

        return $this->functon2("minkjaaa");
    }
    function functon2($ik){
        $this->dom2->load_file("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_si.html");
        $tabelaVremRazm = $this->dom2->find('table[@class="online"]');    // this line trows an error, becaus dom2 is empty
        return"";
    }
}
?>


和trowen错误:
[Fatal error: Call to a member function find() on a non-object in C:\..\simple_html_dom.php on line ..]

1 个答案:

答案 0 :(得分:1)

Find("SELECTORS", index) Find返回一个对象数组,除非指定了一个可选的索引,然后它将返回第N个对象......这可能就是你所缺少的......这是一个有效的例子:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$url = "http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_si.html";

//Create a DOM object
$dom2 = new simple_html_dom();
// Load HTML from a string
$dom2->load_file($url);


// Find the link with the appropriate selectors
$table = $dom2->find('table.online', 0);


// Find succeeded
if ($table)
    echo $table->outertext;
else
    echo "Find function failed !";


// Clear DOM object (needed essentially when using many)
$dom2->clear(); 
unset($dom2);

Online DEMO