I want to declare some php functions and i would like to call those functions in various places in magento.Usually in my core php projects i'm declaring php functions in functions.php and i include that file in all 开发者_运维百科pages.I'm not familiar with MVC structure.So where can i declare these kind of functions.
Thanks
Edit :-
Mango_Myfunc.xml (app/etc/modules)
<?xml version="1.0"?>
<config>
<modules>
<Mango_Myfunc>
<active>true</active>
<codePool>local</codePool>
</Mango_Myfunc>
</modules>
</config>
Config.xml(app/code/local/Mango/Myfunc/etc/configure.xml)
<?xml version="1.0"?>
<config>
<modules>
<Mango_Myfunc>
<version>0.1.0</version>
</Mango_Myfunc>
</modules>
<global>
<helpers>
<Myfunc>
<class>Mango_Myfunc_Helper</class>
</Myfunc>
</helpers>
</global>
</config>
Data.php(app/code/local/Mango/Myfunc/helper/Data.php)
class Mango_Myfunc_Helper_Data extends Mage_Core_Helper_Abstract
{
public function short_str ($str, $len, $suf = '...') {
if (strlen($str) > $len)
return substr($str, 0, $len - strlen($suf) ) . $suf;
return $str;
}
}
This is what i added
i used bellow one to call the function in list.phtml
echo $this->helper('Myfunc/Data')->short_str("test","3"); got the error
Fatal error: Class 'Mage_Myfunc_Helper_Data' not found
Magento has helper classes for those kind of methods. So make your extensions and add your methods and you can then later call them like follows
Mage::helper('yourextension/yourhelper')->yourMethod();
Or you can make a library class out of your common methods.
精彩评论