MageNewbie
MageNewbie

Reputation: 183

getCategories method calling itself?

I'm a little confused as to what is going on here, it looks to me like a method is calling itself? I'm trying to learn about Magento's models. I was working my way back from a helper (catalog/category) and I got to a call on this method "GetCategories". I don't know whats going on here. If anyone could shed light on this code snippet I greatly appreciate it.

  getCategories ( $parent,
          $recursionLevel = 0,
          $sorted = false,
          $asCollection = false,
          $toLoad = true     
              ){
                  $categories = $this->getResource()
                   ->getCategories($parent, $recursionLevel, $sorted, $asCollection,  $toLoad);
                   return $categories;
                  } 

Upvotes: 0

Views: 209

Answers (2)

Slayer Birden
Slayer Birden

Reputation: 3694

Not much to add to @hakra's answer. Just a portion of Magento-specific logic. So to work with Magento models you should know, that Magento has 2 types of Models: normal models, and resource models (we can call assign Blocks to the models too, as a view models - but that is more connected to the V part of MVC).

The resource models were created as a DB adapters that contain only DB-related logic, and often are connected to some DB table, hence contain the logic for CRUD operations with that table. So you'll see smth like this regularly - for the simplicity someMethod is a part of normal model, but since it contains DB-related logic, all the implementation of the method was moved to the resource model, so the body of someMethod in the regular model will be something like that:

public function someMethod($args)
{
    return $this->getResource()->someMethod($args);
}

Upvotes: 1

hakre
hakre

Reputation: 198199

It is hard to say for the code you've posted. Even both methods share the same name (getCategories) it must not mean that they are of the same class or even object.

If you want to find out you would need to compare:

 var_dump($this === $this->getResource());

Apart from that, it is also common in programming recursion that a method calls itself, hence recursion. However for that chunk of code, it would run against the wall.

So technically speaking I would do the assumption that in your example this is not the exact same object method.

Please take note that this answer is independent to Magento, it's just how PHP works generally.

Upvotes: 0

Related Questions