从PHP中的另一个函数中调用函数,但是在不在对象上下文中时使用$ this得到错误

I have the code below: from the getData function im trying to call get_xml within the same class, but I get the error Using $this when not in object context . I have been programming quite some time, so maybe my brains shut off, but imho this should work right? What am I missing here?

class modDataHelper {

    function getData($rid) {
        $xml = $this->get_xml('http://www.dataserver.nl/rss/feed.php?id=' . $rid, 60000);

        return $xml;
    }

    function get_xml($url, $max_age) {
        $file = 'cache/' . md5($url);

        if (file_exists($file)
                && filemtime($file) >= time() - $max_age) {
            // the cache file exists and is fresh enough
            return simplexml_load_file($file);
        }

        $xml = file_get_contents($url);
        file_put_contents($file, $xml);
        return simplexml_load_string($xml);
    }

}

In another file I call

$data = modDataHelper::getData(19464);

You are calling a static method by using ::.

There is no $this in static context use self.

When to use self over $this?

class modDataHelper {

    static function getData($rid) {
        $xml = self::get_xml('http://www.dataserver.nl/rss/feed.php?id=' . $rid, 60000);

        return $xml;
    }

    static function get_xml($url, $max_age) {
        $file = 'cache/' . md5($url);

        if (file_exists($file)
                && filemtime($file) >= time() - $max_age) {
            // the cache file exists and is fresh enough
            return simplexml_load_file($file);
        }

        $xml = file_get_contents($url);
        file_put_contents($file, $xml);
        return simplexml_load_string($xml);
    }

}

Use Ghommey's solution or...

$mdh = new modDataHelper;
$data = $mdh->getData(19464);

Also take a look of this

You are calling getData statically, i.e. there is no object instance, no this.

You need to do something like:

$data = new modDataHelper();
$data->getData(10464)

Or if you want to use static methods you need to declare them as "static" and use "self::", instead of "$this->".

For more info see here.