Question

I have a Yii app that I know the app has been working on a live environment. When I come to running it on a local environment I get the following error:

"Use of undefined constant home - assumed 'home'"

This is the code that is runnning before the error is thrown:

        if(isset($pageID)){
        $page = Page::model()->findByPk($pageID);
    } else {
        $url = Yii::app()->getRequest()->getQuery('action');
        if(isset($url)){
            $page = Page::model()->findByAttributes(array('url'=>$url));
        } else {
            $page = Page::model()->findByPk(2);
        }
    }

The line:

$page = Page::model()->findByPk(2);

... is highlighted in the stack trace. After commenting out everything aside from this line, the notice is still served. I suspect this means the problem is with model code Page.php.

It's a straight-forward bit of code that works on the live server no problem.

Yii is good at detailing it's errors and highlights the actual area that breaks:

/yii/framework/db/ar/CActiveRecord.php(386)

  * }
  * </pre>
  *
  * @param string $className active record class name.
  * @return CActiveRecord active record model instance.
  */
 public static function model($className=__CLASS__)
 {
     if(isset(self::$_models[$className]))
         return self::$_models[$className];
     else
     {
         $model=self::$_models[$className]=new $className(null);
         $model->_md=new CActiveRecordMetaData($model);
         $model->attachBehaviors($model->behaviors());
         return $model;
     }
 }

I don't in all honesty understand what this error means or what's causing it. I've tried all sorts of things to rectify or further understand what's going wrong and I'm drawing a blank. It doesn't appear to be connectivity to the database (which has been correctly amended to local settings). .htaccess seems fine, mod-rewrite it turned on. The config.php file calls in the necessary models etc. Not sure where to go from here.

Why is this error being thrown and how do I go about fixing it?

Update

I've isolated the problem within model Page.php

The notice doesn't load after omit some contants such as:

const HOME=home;    
const ABOUT=about;

I assume that this didn't occur before because I was using an older version of MAMP or different php.ini setup.

Setting

error_reporting = E_ALL & ~E_NOTICE 

within php.ini cleared the error.

Still left wondering why PHP doesn't like these constants defined in this way and what the correct way is.

Was it helpful?

Solution

This kind of error (or, to be precise: this kind of notice) is most often the result of omitting $ before a variable name. If there's home where $home should be, the interpreter treats it like a constant, but since such constant hasn't been previously defined, notice is thrown and string value assumed ('home' in this case).

The reason you're not getting the notice on your other machine is most likely due to notices being suppressed there.

The snipped you pasted doesn't look like it could be responsible for this (especially since it's from one of Yii classes); are you sure you're not pasting code from somewhere higher in the entire stack trace?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top