如何了解某些对象可以访问的方法?
我有以下代码:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'status'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($catid));
$_helper = $this->helper('catalog/output');
foreach ($_productCollection as $_product):
echo $_product->getProductUrl();
endforeach
我想了解如何发现可以在对象上使用的方法。
例如 $_product->getProductUrl() 使用 getProductUrl() 方法来获取 url,但我需要价格并且不知道什么方法调用它。使用 print_r 并不能提供足够的信息让我发现它们是什么。我认为它们位于 MAGE 核心的控制器中。我有商业错误,我厌倦了查看:http://docs.magentocommerce.com/ 但我有时发现自己迷失了。
有谁知道这方面的好教程或者可以给我指导来解决这个问题?
I have the following code:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'status'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($catid));
$_helper = $this->helper('catalog/output');
foreach ($_productCollection as $_product):
echo $_product->getProductUrl();
endforeach
I want to learn how I discover the methods I can use on an object.
For example $_product->getProductUrl() is using the method getProductUrl() to get the url, but I need price and have no idea what method calls that. Using a print_r doesn't provide enough info for me to discover what they are. I presume they are in controllers that are located in the MAGE core. I have commerce bug and I have tired looking at: http://docs.magentocommerce.com/
But I find myself lost at times.
Does anyone know a good tutorial on this or can give me direction to figuring this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,在模型和块中,任何带有
get
或set
的方法可能实际上是从对象的_data 中提取的魔术方法数组。您可以看到像这样的对象中的所有数据
因此,如果该数组有一个名为
some_data
的键,您可以调用名为getSomeData
的方法,但请记住,某些方法将具有 <以
get
和set
开头的strong>actual 方法,因此请务必检查类定义。其次,您可以使用 PHP 反射函数(或更完整但复杂的 PHP Reflection Class API)来查看对象是什么类,然后获取该类的方法列表
首先,使用 get_class 获取对象类的名称。
然后,传递 get_class_methods 来获取对象上所有可调用方法的列表
这将为您提供所有类方法的列表。然后,您可以使用 Commercebug bug 的 Class/URI Lookup 选项卡快速将类所在的文件归零定义来查看方法定义。请记住,一些方法将在祖先类中定义。花时间学习 IDE 或像 ctags 这样的程序是非常值得的投资,他们会让您可以快速跳转到各个类定义。
First, in models and blocks, any method with
get
or aset
may actually be a magic method that's pulling from the object's_data
array. You can see all the data in an object like thisSo if this array had a key named
some_data
, you could call a method namedgetSomeData
Remember though, some methods will have actual methods that start with
get
andset
, so always check the class definition.Secondly, you can use PHP reflection functions (or the more complete but complicated PHP Reflection Class API) to see what class an object is, and then get a list of methods on that class
First, use get_class to get the name of an object's class.
Then, pass that get_class_methods to get a list of all the callable methods on an object
This will give you a list of all the class methods. You can then use the Class/URI Lookup tab of Commercebug bug to quickly zero in on which file a class is defined in to look at the method definitions. Remember, some methods will be defined in ancestor classes. Investing the time to learn an IDE or a program like ctags is well worth the investment, ad they'll let you quickly jump to individual class definitions.
以下是查找类文件的简单说明:-
1. Collection
上面的代码表示我们正在调用产品集合类。类文件可以在以下位置找到:-
在 Collection.php 中你会找到以下类:-
所以,你需要的函数可以在
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
> 类。如果它不存在,那么它可能位于父类中,即
Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
2.模型
同样,
对于上面的代码,类文件是:-
3. Helper
对于帮助文件,
上面的帮助文件是:-
4.最后
最后,使用 Eclipse 或 Netbeans 这样的 IDE 将帮助您快速高效地学习和编写代码。
Here is a simple direction to find the class files:-
1. Collection
The above code means that we are calling the product collection class. The class file can be found in:-
In Collection.php you will find the following class:-
So, your required function can be in
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
class.If it is not there then it can be in the parent class, i.e.
Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
2. Model
Similarly,
For the above code, the class file is:-
3. Helper
For helper file,
The above helper file is:-
4. Finally
Finally, using an IDE like Eclipse or Netbeans will help you learn and write code quickly and efficiently.
我完全是新手,但我使用此页面上的信息来获取某个对象可用的所有方法并将它们发送到日志。
例如类别_集合:
I'm a total novice, but I used the info on this page to get all the methods available to me for a certain object and send them to a log.
For example the category_collection: