I am defining a new block to clean up my view like so:
class Jamie_Utilities_Block_Myblock extends Mage_Core_Block_Template
{
public function getProducts()
{
$products = array(
new Product('Product Name', "image source"),
new Product('Product Name', "image source"),
);
return $products;
}
}
I would like to create a little Product class in the same file to hold a couple fields. Myblock.php now looks like:
class Jamie_Utilities_Block_Myblock extends Mage_Core_Block_Template
{...}
class Product
{
public $name;
public $image;
public function __construct($name, $image)
{
$this->name = $name;
$this->image = $image;
}
}
This works, but is it a good thing to do? Should I be declaring all my classes as models in the module’s config.xml or should I just use a better class name to avoid conflicts e.g. Jamie_Utilities_Block_Product?
The
<models>node in your config is a class prefix, not a class name.An example config of
<config><global><models><your_module><class>Yer_Module_Model</class></...>is what’s found when this code is parsed:Mage::getModel('your_module/rest_of_class_name');. The first part is converted to yer_module_model, and the rest is tacked on with a leading underscore, i.e.yer_module_model_rest_of_class_name. According to the autoloader, the system will look for that definition inYer/Module/Model/Rest/Of/Classname.php.The short story is this: define your class prefix and you can create as many classes as you want.