Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Interesting- Members on a Magento Object
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Hi all,

With all the threads I had logged over the past one year behind me, I had a queer doubt that caught me today. Thought I would log a thread and share my thoughts on this one as I explore the answer to it.

I observe that most of the objects defined by Magento donot explicity mention their members which is a somewhat strange way of Object Oriented Programming. The end user is never aware of the members nor the accessors to the members.

For example. Take the case where we use
‘Mage_Checkout_Model_Session’ in class ‘Mage_Checkout_Block_Onepage_Success.getOrderId()’

if you observe ‘orderId’ is not an explicit member of ‘Mage_Checkout_Model_Session’ class. And neither is its accessor method getOrderId().

Similarly you would usage of ‘Mage_Catalog_Model_Product’ for product related attributes.

I do believe that this offers flexibility of adding attributes to an object, but for a programmer, it becomes a nightmare to find the members of an object. I am right now trying to figure out if there is any easy way to determine the members on a Magento Object. Will keep this thread posted.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

This is probably the result of long parent-child relationships and having multiple people work on a project rasberry

PHP unfortunately is not strongly typed, which makes it a pain to see exactly what is going on sometimes.

Couple that the confusion added by the use of PHP’s magic methods (__get, __set, __autoload, __call), and you can potentially have a very steep learning curve.

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

ok ...here we go smile

Think I found the way of getting at the members of a Magento Object (or rather Entity) -
Let me take an example of finding the members of “Mage_Sales_Model_Order”

Execute the below query to determine the attributes of a Magento Order.

SELECT attr.attribute_code
FROM sto_eav_attribute attr
, sto_eav_entity_type type
WHERE type.entity_type_id = attr.entity_type_id
and entity_type_code = ‘order’;

The attributes that belong to an Order, I believe are dynamically added as members of the “Mage_Sales_Model_Order” class. So incase you need to retrieve the total price of an order, execute the above query, find the attribute to be “grand_total” and use the accessor method getGrandTotal() on the Order object.

Hope this helps all smile!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
mike222
Member
 
Total Posts:  41
Joined:  2008-04-07
Austria
 

But this is only one half of the story.

Check the magic function
public function __call($method, $args)
in lib/Varien/Object.php

Nearly every class is inherited from there.

Here you can find an array called _data.

The magic function will be called by php whenever a method call cannot be resolved by the interpreter itself. Varien uses this to check if a key with similar name exists in the _data array and returns the corresponding value.

e.g. When calling a method object->getSomeValue() on any object, php cannot find that method and will call the function __call("getSomeMethod", null)

Varien recognizes that this is a get… method, translates the rest to “some_value” and checks the array _data for this key. If found it returns the corresponding value and if not it returns null.

While this eventuelly makes sense for user-defined attributes (however a simple getAttribute($name) would do the same job), it is truly horrible to use it so heavily. From my point of view, Moshe and his team should consider at least to minimize the use of the magic functions in the next few releases.

So if you really want to know which methods are possible, consider using print_r(object->getData()); or similar constructs.

Hope this helped a little.

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

That was a very valid point and suggestion of using print_r() or var_dump() should be very useful. Thanks smile.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top