I am new in PHP as well as in Magento. I have some questions regarding factory methods and fundamental of PHP of OOPS. As if we want to call a method in php we instating the class and we call a function and property.

For Example

class A {

  public $a = "some text";

  public function fn(){

   $b = "this is another text";

$B = new A;

Then we can call the function and member like $B->a; and $B->fn();

But in static mathod we call it with class name and then method name using :: scope resolution method.

When I check some functions in Magento in which functiona are calling one by one like..

$categories = Mage::getModel('catalog/category')

I checked in Mage.php file in which getModel() is static method but how this is calling getCollection() method? Can any static method can call another static method of the same class or members? If yes then other static function are not available in Mage.php file?

Till now I have seen only single method calls not seen multiple method call. Please any one can clear my doubts here.

I am sorry if it is silly a question but I dont want to ignore the concept if I am missing somewhere.

Thank you all in advance :)


In short, the answer here is that the factory Mage::getModel returns the actual object (catalog/category, in your example, which maps to Mage_Catalog_Model_Category).

The object, now an rvalue, is available for you to invoke any method on in immediately. In your example, this is getCollection. If getCollection returns $this, you may then use the fluent interface (method chaining).

Another concept at play here is that Mage::getModel performs object instantiation for you, so you should never have to do new A, as in your example. This is a feature of Magento, and class files are found and instantiated via the autoloader. Magento's autoloader will locate the class file in multiple code pools so that you will never have to explicitly include them in your code.


In your example of class A, you would be able to use fluent interfaces if any of your methods return an instantiated version of the A class:

class A {

  public $a = "some text";

  public static function getModel() { 
      $obj = new A();
      return $obj;

  public function fn($text = 'this is another text'){

    $this->a = $text;
    return $this;

And can be used like

$class = A::getModel()->fn('foo')->fn('bar')->fn('bat')->fn('baz');
| improve this answer | |
  • Wow..really got the good answer from you. In this example, we are returning object so we can use chain it one by one. I hope, I understood well. Please correct me if I am wrong. After seeing your nice answer, I checked Magento files once again. I checked Mage.php file. Where public static function getModel($modelClass = '', $arguments = array()) { return self::getConfig()->getModelInstance($modelClass, $arguments); } – MagentoBoy May 29 '13 at 5:39
  • getConfig() returns self::_config, which in itself is set via self::$_config = new Mage_Core_Model_Config($options); -- in short, it's exactly how I demonstrated to you in your own A class how it could be done, but abstracted one step further because it needed to return a property instead of directly instantiating. – philwinkle May 30 '13 at 15:58
  • @philwinkle.. Nice answer :).. I think I am so close to my answer. One more question Please. Where $_config has defined its value [code] $_config = new Mage_Core_Model_Config($options) [/code] .. Thank you very much to giving me exact answer what i am looking for. – MagentoBoy Jun 1 '13 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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