为什么我不能在 getInfFromTheSource()、selectWhatIsNeeded() 函数中传递 $open_config 变量。据我了解,为此,您需要将它们传递给构造函数并以下面代码中的方式处理它们。
class Downloader
{
public $source_link, $source_html, $dom, $xpath, $open_config;
function __construct($source_link, $source_html, $dom, $xpath, $open_config)
{
$this->source_link = $source_link;
$this->source_html = $source_html;
$this->dom = $dom;
$this->xpath = $xpath;
$this->open_config = $open_config;
}
public function getInfFromTheSource ()
{
$open_config = json_decode(file_get_contents('config.json', true));
$source_link = $open_config->InfSource;
$source_html = file_get_contents($source_link);
return($source_html);
}
public function selectWhatIsNeeded ($source_html)
{
$open_config = json_decode(file_get_contents('config.json', true));
$xpathstring = $open_config->string_for_xpath;
$dom = new DOMDocument();
$dom->loadHTML($source_html);
$xpath = new DOMXpath($dom);
$archives_names = $xpath->query('$xpathstring');
return($names);
}
也许重点是我如何创建类的实例并访问方法?我将类文件包含在 required_once 中,然后:
$obj = new Downloader($source_link, $source_html, $dom, $xpath, $open_config);
$obj->selectWhatIsNeeded($obj->getInfFromTheSource());
我尝试将 $open_config 声明为全局的,我完全按照程序中的方式进行了操作,并且可以正常工作。但由于某种原因,它不能那样工作......大师,如果你有时间,你能建议最好的方法吗?
并在访问方法中
$this->open_config