Why am I getting the error "Calling a member function on a non-object"?

I am working with a Zend database supported by MySQL and am trying to customize the index view. However, with the code that I have, I continue to receive a “member function call by non-object” error, and I don’t know how to solve it. Here is the Controller for my index page:

 public function indexAction() {
     $dImage = new Application_Model_DImage();
 }

Here is the code for the DImage model that it refers to:

public function getImageDesCol ($Id) {
    return $this->getDb()->fetchCol("SELECT description FROM d_image WHERE d_id = ?", $Id);
}

Finally, here is the source of my index view page, where I am trying to display values ​​from a DImage model:

  <?php $description = $this->dImage->getImageDesCol($id) ?>
  <label><?php echo $description[0] ?></label>

When I load this page, I get the message “Calling a member function getImageDesCol()for a non-object” instead of the displayed page. Since I am new to the Zend framework and PHP in general, I feel that there is something obvious that I am missing. What can I do to fix this?

+3
2

Application_Model_DImage , . :

public function indexAction()
{
    $dImage = new Application_Model_DImage();
    $this->view->dImage = $dImage;
}

$dImage .

Zend_View, Application_Model_DImage ( Zend_Controller_Action). , , . Zend_Controller_Action ().

+2
source

Edit:

 public function indexAction()
 {
     $dImage = new Application_Model_DImage();
 }

To:

 public function indexAction()
 {
     $this->dImage = new Application_Model_DImage();
 }
+3
source

All Articles