1

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);
Hans Wassink
  • 2,529
  • 3
  • 30
  • 45
  • fixed it by calling `$xml = self::get_xml('http://www.dataserver.nl/rss/feed.php?id=' . $rid, 60000);` Thanks guys for helping me understand AND give me the answer! – Hans Wassink Mar 22 '12 at 10:47
  • 1
    Its calling statically using scope resolution operator "::" rather than "->" and self keyword stands for "calling classes own function hence 'self'" – OM The Eternity Mar 22 '12 at 10:52
  • Not for the points, but i think the Q&A could use some upvotes, because google is not really spitting out clear answers, at least not as clear as yours! – Hans Wassink Mar 22 '12 at 10:56

3 Answers3

4

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);
    }

}
Community
  • 1
  • 1
jantimon
  • 36,840
  • 23
  • 122
  • 185
  • 1
    I didn't think PHP even allowed calling a non-static method statically anymore without at the very least throwing a warning. – GordonM Mar 22 '12 at 10:49
1

Use Ghommey's solution or...

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

Also take a look of this

Community
  • 1
  • 1
Wh1T3h4Ck5
  • 8,399
  • 9
  • 59
  • 79
  • I thought of this, but that kinda felt dirty from inside a function of the same class... – Hans Wassink Mar 22 '12 at 10:55
  • 1
    From inside function of the same class, you can use `$data = $this->getData(19464);` where `$this` refers to current object and `getData()` to object's method. – Wh1T3h4Ck5 Mar 22 '12 at 10:58
  • Ah, I see what you mean now, you refered to my last line of code, not to the getData func. Tata – Hans Wassink Mar 22 '12 at 11:00
  • In fact, `$xml = $this->get_xml(...` is OK but in this case you can't call `getData` method as static, using **(::)** operator. – Wh1T3h4Ck5 Mar 22 '12 at 11:05
1

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.

Envyrus
  • 315
  • 1
  • 3
  • 10