Here is the scenario:
You are working with some model from magento and you want to know which methods are available to you to retrieve the various parameters of an object in magento.
Lets do an example:
$order = `Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());`
In this example we are working with an order object.
From doing searches on various topics I’ve found that there are a number of different methods available from this particular object for example:
getCustomer()
getCustomerName()
getGrandTotal()
getId()
I will assume (maybe incorrectly) that the top 3 example methods are actually defined within mage_core_model_sales_order class or one of the classes that it extends
The getId() is a special case.
It is an automagical method that works with the __call() magic method to return data from an object even when the method is not defined anywhere.
please explain the naming conventions for the Magento automagical methods
and what is the easiest way to determine all of the available automagical methods available from a given model or collection.
In brief (since this is documented in various other places on the internet), magic methods are for setting and getting data. These calls
are equivalent to these calls
The string
'some_thing'is a data key. To set and get data with a magic method is this key is CamelCased into ‘SomeThing’.You can see all the data properties (and therefore derive the magic methods) with a parameterless call to ‘getData’
Be careful with
var_dumpif you’re not usingxDebug, and many Magento objects are too large and/or too circular to fully display. If you’re running into that problem, you can just peek at the keys withThat’s all magic methods are for in Magento — getting and setting data.
Play around with this using a varien object, which is the object all Models and Blocks inherit from in Magento.
One last thing to keep in mind: If an object’s class has a method defined with the same name as a magic method,the magic method is ignored. Sometimes the only way to know for sure if something is a magic method or a real method is to go look at the class definition.