views:

50

answers:

2

Hi! I am getting "Fatal error: Call to undefined function getSContent()" with the included code but... as you can see: My function is right there!

Please, help. I am sure I need some sleep.

class InfoController extends AppController {

    var $name = 'Info';
    var $helpers = array('Html', 'Session');
    var $uses = array();

    function display() {
        $path = func_get_args();

        $section = $path[0];
        $mainMenuActiveElement = $section;
        $sectionContent = getSContent($section);

        $this->set(compact('section', 'mainMenuActiveElement', 'sectionContent'));
        $this->render('/pages/info');
    }

    function getSContent($section) {
        $sectionContent = '';
        switch ($section) {
            case 'bases':
                $sectionContent = 'some content';
                break;
            case 'informacion':
                $sectionContent = 'some other content';
                break;
        }
        return $sectionContent; 
    }

}
+7  A: 

You have to specify the context. PHP unlike e.g. c++ doesn't assume this implicitly.

$sectionContent = $this->getSContent($section);
VolkerK
Oh! I really need to go and get some sleep. Thank you @VolkerK!
vmarquez
And I'm really sorry for the "doesn't assume ... implicitly" part ;-) See http://en.wikipedia.org/wiki/Name_resolution
VolkerK
@VolkerK It wouldn't make sense in PHP. See http://stackoverflow.com/questions/3345408/what-is-the-point-of-having-this-and-self-in-php/3376490#3376490
Artefacto
@Artefacto: I never said it would, did I? (nor did I say it wouldn't ...simply didn't mentioned it at all ;-))
VolkerK
+2  A: 

Your function is, in particular, a method. Method names do not live in PHP's functions table, they are associated with a particular class.

You should call them with the notation $object->methodName(), where $object can be $this when inside a method in order to call an instance method of the same instance as the current one.

The notation becomes ClassName::methodName() when the method is declared static. In special circumstances, instance methods call also be called with this syntax.

Artefacto