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);
3
  • 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! Commented Mar 22, 2012 at 10:47
  • 1
    Its calling statically using scope resolution operator "::" rather than "->" and self keyword stands for "calling classes own function hence 'self'" Commented Mar 22, 2012 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! Commented Mar 22, 2012 at 10:56

3 Answers 3

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't think PHP even allowed calling a non-static method statically anymore without at the very least throwing a warning.
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.

Comments

1

Use Ghommey's solution or...

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

Also take a look of this

4 Comments

I thought of this, but that kinda felt dirty from inside a function of the same class...
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.
Ah, I see what you mean now, you refered to my last line of code, not to the getData func. Tata
In fact, $xml = $this->get_xml(... is OK but in this case you can't call getData method as static, using (::) operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.