Basic question about Magento Programming
I found many code snippets in Magento such as
$variable_name = Mage::app()->function_name();
here's an example
$websites = Mage::app()->getWebsites(true, true);
What does this function call (calls?) do? The syntax is unfamiliar, and I need some basic explanation so I can better tra开发者_如何学编程ce code.
That's actually two separate method calls.
$result = Mage::app()->getWebsites();
The first is
$o = Mage::app();
This is calling the static method app
on "class Mage", which can be found in
app/Mage.php
The ::
operator calls static class methods. If you don't understand what that means, just think "class on the left, method on the right, and you can't use a $this variable inside the method".
So, this method call returns an object, which will almost certainly be a Mage_Core_Model_App
, which is at
app/code/core/Mage/Core/Model/App.php
So the one liner above could be rewritten as
$o = Mage::app();
$result = $o->getWebsites();
Hope that helps demystify things a bit.
Get yourself a good IDE like Eclipse with PDT. It's code completion tools are a godsend.
For example when you type MA it completes it to Mage::
automatically and provides a list of static functions, including app()
. After that type ->GETW and it brings up another list of functions, getWebsites()
will be selected as you type and when highlighted will explain what parameters are required and an overview (gathered from PHPDoc) that explains what it does.
If you had tools like this you would never be asking "What actually this function call do??" on websites.
One thing you might be seeing is the "magic getters and setters" in the zend framework. I know it confused me for a bit. In the zend framework, you can call gets and sets without there being and actual function defined anywhere, if there's not function definition then it just attaches whatever you're assigning to the object calling the set.
You are setting a variable to contain whatever a function returns. To see what this method exactly returns you have to dump the value or examine the method. By the looks of it it will set the value to contain websites object that can later be iterated or accessed by this variable
精彩评论