Magento 1.9 – Call to a Member Function sayHello() on Boolean Error

magento-1.9PHP

I am new to Magento and following tutsplus tutorial. During module development I got this error "Call to a member function sayHello() on boolean".

Below is my test.php file in magento(for testing the module):

     require_once'app/Mage.php';          
     Mage::app();
     $customer= Mage::getModel("customer/session");

     //Mage_catalog_Model_Product

     Mage::getModel("catalog/product");

     $product = Mage::getModel("demo/product");

     $product -> sayHello();

Here is the model file code (Product.php)

      class Tutsplus_Demo_Model_Product {

       public function sayHello(){

       echo "Hi";
      }
     }

When I removed the $product->sayHello(); to // echo get_class($customer); it shows the class name. config.xml file is here.

     <config>
       <global>
         <models>
            <demo>
                <class>Tutsplus_Demo_Model</class>
            </demo>
         </models>
       </global>
      </config>

Best Answer

app/etc/modules/Tutsplus_Demo.xml

<?xml version="1.0"?> 
 <config> 
   <modules> 
    <Tutsplus_Demo> 
     <active>true</active> 
     <codePool>local</codePool> 
     <version>0.1.0</version> 
    </Tutsplus_Demo> 
   </modules> 
 </config>

make sure you define model in app/code/local/Tutsplus/Demo/etc/config.xml

<?xml version="1.0"?>
    <config>
    <modules>
    <Tutsplus_Demo>
      <version>0.1.0</version>
    </Tutsplus_Demo>
  </modules>
    <global>
        <models>
            <demo>
                <class>Tutsplus_Demo_Model</class>
            </demo>
        </models>
     </global>
    </config>

finally your model app/code/local/Tutsplus/Demo/Model/Product.php

capitalize the first keyword and extends to Mage_Core_Model_Abstract

class Tutsplus_Demo_Model_Product extends Mage_Core_Model_Abstract
{
      public function sayHello(){

       echo "Hi";
      }

}

for calling the model

 $product = Mage::getModel("demo/product");
   $product->sayHello();
Related Topic