Create a simple model class in Magento

Assume we have a Helloworld module under MyExtensions package, and we want to create a simple model class and a hello function in this model to print hello world.
1. Add the below in the global tag in Magehome/app/code/local/MyExtensions/etc/config.xml
[code language=”xml”]
<models>
<helloworld>
<class>MyExtensions_Helloworld_Model</class>
</helloworld>
</models>
[/code]

2. Create a file at Magehome/app/code/local/MyExtensions/Model/Utilities/Hello.php
[code language=”php”]
<?php
class MyExtensions_Helloworld_Model_Utilities_Hello
{
public function hello()
{
echo "Hello World!!!";
}
}
[/code]

3. Now we can call this hello function anywhere in Magento like this:
[code language=”php”]
Mage::getModel(‘helloworld/Utilities_Hello’)->hello();
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search