如何学习某些对象可以访问的方法? [英] How do I learn the methods that can be accessed for certain objects?

查看:67
本文介绍了如何学习某些对象可以访问的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$_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.

例如$ _product-> getProductUrl()正在使用方法getProductUrl()来获取url,但是我需要价格,并且不知道该调用哪种方法.使用print_r不能为我提供足够的信息来发现它们是什么.我假设它们位于MAGE内核中的控制器中.我有商务错误,也很累于查看: http://docs.magentocommerce.com/ 但是我发现自己有时会迷路.

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?

推荐答案

首先,在模型和块中,任何带有getset 的方法实际上可能是吸引人的魔术方法.从对象的_data数组中获取.您可以在这样的对象中查看所有数据

First, in models and blocks, any method with get or a set 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 this

var_dump($object->getData());

因此,如果此数组具有名为some_data的键,则可以调用名为getSomeData

So if this array had a key named some_data, you could call a method named getSomeData

echo $object->getSomeData();

不过请记住,某些方法将具有以getset开头的 actual 方法,因此请始终检查类定义.

Remember though, some methods will have actual methods that start with get and set, so always check the class definition.

第二,您可以使用PHP反射函数(或更完整但更复杂的PHP Reflection Class API)来查看对象是什么类,然后获取该类的方法列表

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

首先,使用 get_class 获取对象类的名称.

First, use get_class to get the name of an object's class.

$class_name = get_class($object);

然后,传递该 get_class_methods 以获得对象上所有可调用方法的列表

Then, pass that get_class_methods to get a list of all the callable methods on an object

$class_name = get_class($object);
$methods = get_class_methods($class_name);
echo "Methods for class $class_name \n<br />\n";
foreach($methods as $method)
{
    var_dump($method);
}

这将为您提供所有类方法的列表.然后,您可以使用Commercebug错误的类/URI查找" 标签,快速将定义了该类的文件归零以查看方法定义.请记住,某些方法将在祖先类中定义.花时间学习IDE或像ctags之类的程序是值得投资的,它们可以让您快速跳入各个班级定义.

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.

这篇关于如何学习某些对象可以访问的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆