Difference Between getData(‘name’) and getName() in Magento 1.8

magento-1.8model

I am using magento 1.8

And I am facing problem when I use below methods

$product->getData('name');

$product->getName();

Is they are same,
I sure that they return same value[answer].
When I am using I am getting error

Fatal error: Call to a member function getData() on a non-object in

So may i know the exact difference between these two function.

Thank You,

Best Answer

They may be the same or they may be different. It depends on the object you are using.

If the class you instantiate contains the method getName() then the result you get from getName and getData('name') may be different.
You can even get an error if the class does not extend Varien_Object and does not have the method getData.

For classes that extend Varien_Object and do not contain the method getName() the results of the 2 methods are the same.
Varien_Object implements the method __call that is called if a certain method does not exist. See here how the code looks like.

So it basically maps the call of any method getSomethingHere() to getData('something_here'). It works the same for "fake" methods that start with set, has and uns.

More details provided here.

Related Topic