user254799
user254799

Reputation: 75

Controller to View: Call to undefined function

I have a simple class called ForumController that will eventually be used allover my website with quite a few methods in it. For now there is two:

class ForumController extends BaseController { 

    // Parses XML feed to JSON
    public static function parseXML($xml) {
        $file = file_get_contents($xml);
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $file);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        $simpleXML = simplexml_load_string($fileContents);
        $json = json_encode($simpleXML);

        return $json;

    }

    public static function getForums($xml){
        $feed = parseXML($xml);
        return $feed;
    }

}

I'l be using this in views, so I learned that I can call this in an IoC container? So I made an IoC.php and included that in bootstrap.php file.

IoC.php:

<?php 

    View::composer('getForums', 'ForumController');

Now I'd like to call this in my views like so..

{{ ForumController::getForums('http://someForum.com/forum/syndication.php?fid=4,140,22,62,134&limit=5'); }}

But, as usual, I did something wrong. Error:

Call to undefined function parseXML()

This is my first time trying to use another controller besides HomeController so I'm sure I did everything wrong. How do I get the results I want?

Upvotes: 0

Views: 890

Answers (1)

c-griffin
c-griffin

Reputation: 3026

You need to call parseXML() as a class method instead of a function. In this case, using self or static (won't make any difference when you're calling it from the same class it is defined in, however if parseXML() is ever inherited, static will bind it to the class at run-time.

public static function getForums($xml){
    $feed = self::parseXML($xml); // or static::parseXML($xml)
    return $feed;
}

Upvotes: 1

Related Questions