Strange Overloading Behavior

I am experiencing some strange behavior with my app’s controllers, and although I’m sure I am missing something obvious here, I cannot see what the problem is.

Here’s the issue: I added a public property called $pageHeading to the Controller class that all of my controllers inherit from by default. By doing this I was hoping to be able to call $this->pageHeading from the main layout file without having to manually add it to each controller in order to avoid an error message (some don’t need it).

Here is my Controller class:




<?php

/**

 * Controller is the customized base controller class.

 * All controller classes for this application should extend from this base class.

 */

class Controller extends CController

{

    /**

     * @var string The value that will appear in the <h1> tag in the layout file.

     */

    public $pageHeading='';


    /**

     * @var string the default layout for the controller view. Defaults to 'application.views.layouts.column1',

     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.

     */

    public $layout='application.views.layouts.column1';


    /**

     * @var array context menu items. This property will be assigned to {@link CMenu::items}.

     */

    public $menu=array();

    

    /**

     * @var array the breadcrumbs of the current page. The value of this property will

     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

     * for more details on how to specify this property.

     */

    public $breadcrumbs=array();

}



Here is an example from one of the child controllers:




class DegreeController extends Controller

{

    public function init()

    {

        $this->pageHeading = 'blah blah blah';

    }


    // Non-relevant stuff omitted ...

}



And finally, here is the error message I am getting:




CException

Description


Property "DegreeController.pageHeading" is not defined.

Source File


/some/example/directory/protected/controllers/DegreeController.php(24)


00012:     /**

00013:      * @return array action filters

00014:      */

00015:     public function filters()

00016:     {

00017:         return array(

00018:             'accessControl', // perform access control for CRUD operations

00019:         );

00020:     }

00021: 

00022:     public function init()

00023:     {

00024: $this->pageHeading = 'blah';

00025:     }

00026: 

00027:     /**

00028:      * Specifies the access control rules.

00029:      * This method is used by the 'accessControl' filter.

00030:      * @return array access control rules

00031:      */

00032:     public function accessRules()

00033:     {

00034:         return array(

00035:             array('allow', // allow admin user to perform 'admin' and 'delete' actions

00036:                 'actions'=>array('index', 'create', 'update'),


Stack Trace


#0 /some/example/directory/protected/controllers/DegreeController.php(24): CComponent->__set('pageHeading', 'blah')

#1 /some/other/directory/yii-1.1.1.r1907/framework/web/CWebApplication.php(319): DegreeController->init()

#2 /some/other/directory/yii-1.1.1.r1907/framework/web/CWebApplication.php(120): CWebApplication->runController('degree/update')

#3 /some/other/directory/yii-1.1.1.r1907/framework/base/CApplication.php(135): CWebApplication->processRequest()

#4 /some/example/directory/index.php(12): CApplication->run()

#5 {main}






Can anyone see what I am missing here? Thanks in advance!

If you don’t set it in init() then it works I guess?

// I mean, if you set it within an action or in beforeAction() does it work then?

No, I just put it in init() in my post as an example. But I get the same exception, no matter where I set it:





CException


Description

Property "DegreeController.pageHeading" is not defined.




[b]Source File[/b]

/my_yii_app/protected/controllers/DegreeController.php(72)


00060: 

00061:             if ($degree->save())

00062:             {

00063:                 Yii::app()->user->setFlash('success', $this->generateFlashMessage('success', 'create', 'degree', $degree->school->schoolName));

00064:                 $this->redirect(array('admin/degree', 'id'=>$degree->school->id));

00065:             }

00066:         }

00067:         $this->render('create', array('degree'=>$degree));

00068:     }

00069: 

00070:     public function actionUpdate()

00071:     {

00072:         $this->pageHeading = 'blah blah';




Am I just crazy, or is that really strange?

Normally it should work yes. Does any other property work like $layout?

Yeah, $layout works fine - I can both get it and set it.

I figured it out. I use NetBeans a lot, and I like to use it’s source code navigation feature frequently to get around between related files.

So, what happened was this: when I wanted to add the $pageHeading property to the parent Controller class, I opened the file via the navigation feature. However, when NetBeans asked me which Controller class to navigate to (the one in /my_app/protected/components or the generator template in /yii/framework/cli/views/webapp/protected/components), I clicked on the wrong one and subsequently added $pageHeading to the wrong class.

Hehe, sometimes hard to discover such small errors/mistakes. :D

Haha, yeah. Thank God for grep. That’s how I found it.