简体   繁体   中英

Warning: Creating default object from empty value to pass value from controller to view in zend framework

my controller file code

public function viewAction(){
   $this->view->title="View Album";
}

my view file code

<?php echo $this->title;?>

but value can not access in view file warning given

warning

Warning: Creating default object from empty value

First this is not a Zend Framework issue. It is about core PHP.

It is an E_STRICT warning which may be enabled in your error_reporting ini settings if you are using PHP <= 5.3. But as of PHP 5.4 that error mode changed to E_WARNING . You should not disable E_WARNING warnings for the development environment.

So when are you getting that warning? You are getting this while you are trying to use an object which has not defined. Therefore, you have to define that object first, you should then use that property.

In ZF, you have ViewModel() object which has property overloading feature. To get rid of this warning you would use $title thus

public function viewAction()
{
    $view = new ViewModel();
    $view->title = "View Album";

    return $view;
}

And in your view script

<?php echo $this->title; ?>

For more details you may check out this problem and answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM