I've been studying the source code of Magento recently, and I came across this snippet in Varien_Object, which is puzzling me:

 * Convert object attributes to array
 * @param  array $arrAttributes array of required attributes
 * @return array
public function __toArray(array $arrAttributes = array())
    if (empty($arrAttributes)) {
        return $this->_data;

    $arrRes = array();
    foreach ($arrAttributes as $attribute) {
        if (isset($this->_data[$attribute])) {
            $arrRes[$attribute] = $this->_data[$attribute];
        else {
            $arrRes[$attribute] = null;
    return $arrRes;

 * Public wrapper for __toArray
 * @param array $arrAttributes
 * @return array
public function toArray(array $arrAttributes = array())
    return $this->__toArray($arrAttributes);

My understanding is that if I wanted an array of the data within a Varien_Object, I would call the toArray() method. But the impression I get from this code is that __toArray() should be private. Yet it is public.

Can anybody tell me if this is an oversight, or if __toArray is used somewhere else in the Magento codebase?

I thought that perhaps it is a legacy thing, but if that were true, wouldn't there be a note saying so?


This inconsistency gets stranger still. Varien_Object has many methods to export the data:

  • toArray()
  • toXml()
  • toJson()
  • toString()

Of these four, only toArray() has a corresponding public __toArray() method. toXml() and toJson() have protected ones, and toString() has none, except a commented out one, which was public.

On a separate note, Magento seems to use a public wrapper function quite a lot. toHtml() and _toHtml() spring to mind. Why is this? What advantage does using such a function provide when creating a subclass?


1 Answer 1


It is used two times:

$newOption = $this->__toArray();

return $this->__toArray();

So it looks like whether they use it more ore less "protected".

Nevertheless, I think the intention behind the naminig __toArray is, that they want be similar to __toString() ignoring the circumstance, that it has no meaning within PHP. And __toString is public!

  • 1
    Thanks. This comment motivated me to look at the rest of the exporter methods in Varien_Object. See my update for what I found. They aren't consistent with each other at all. Presumably, as a client I'm supposed to ignore the __ methods and just used the unprefixed ones, but it still leaves me wondering about it all. Sep 1, 2013 at 15:47
  • Wow. Ok. This is inconsistency. No idea :) Sep 1, 2013 at 15:54
  • I guess if this is the only weird part in a six year old project the size of Magento, then that's remarkable. It does seem to be pretty clean considering both its age and size. Sep 1, 2013 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.