如何了解某些对象可以访问的方法?

发布于 2024-10-29 18:29:52 字数 753 浏览 2 评论 0原文

我有以下代码:

$_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一直在等你来 2024-11-05 18:29:52

首先,在模型和块中,任何带有 getset 的方法可能实际上是从对象的 _data 中提取的魔术方法数组。您可以看到像这样的对象中的所有数据

var_dump($object->getData());

因此,如果该数组有一个名为 some_data 的键,您可以调用名为 getSomeData 的方法,

echo $object->getSomeData();

但请记住,某些方法将具有 <以getset 开头的strong>actual 方法,因此请务必检查类定义。

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

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

$class_name = get_class($object);

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

$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 bug 的 Class/URI Lookup 选项卡快速将类所在的文件归零定义来查看方法定义。请记住,一些方法将在祖先类中定义。花时间学习 IDE 或像 ctags 这样的程序是非常值得的投资,他们会让您可以快速跳转到各个类定义。

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());

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

echo $object->getSomeData();

Remember though, some methods will have actual methods that start with get and set, 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.

$class_name = get_class($object);

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);
}

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.

陌路黄昏 2024-11-05 18:29:52

以下是查找类文件的简单说明:-

1. Collection

$_productCollection = Mage::getResourceModel('catalog/product_collection')

上面的代码表示我们正在调用产品集合类。类文件可以在以下位置找到:-

app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Product\Collection.php

在 Collection.php 中你会找到以下类:-

class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
    extends Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract

所以,你需要的函数可以在 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection > 类。

如果它不存在,那么它可能位于父类中,即 Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract

2.模型

同样,

$_product = Mage::getModel('catalog/product')

对于上面的代码,类文件是:-

app\code\core\Mage\Catalog\Model\Product.php

3. Helper

对于帮助文件,

$_helper = $this->helper('catalog/output');

上面的帮助文件是:-

app\code\core\Mage\Catalog\Helper\Output.php

4.最后

最后,使用 Eclipse 或 Netbeans 这样的 IDE 将帮助您快速高效地学习和编写代码。

Here is a simple direction to find the class files:-

1. Collection

$_productCollection = Mage::getResourceModel('catalog/product_collection')

The above code means that we are calling the product collection class. The class file can be found in:-

app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Product\Collection.php

In Collection.php you will find the following class:-

class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
    extends Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract

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,

$_product = Mage::getModel('catalog/product')

For the above code, the class file is:-

app\code\core\Mage\Catalog\Model\Product.php

3. Helper

For helper file,

$_helper = $this->helper('catalog/output');

The above helper file is:-

app\code\core\Mage\Catalog\Helper\Output.php

4. Finally

Finally, using an IDE like Eclipse or Netbeans will help you learn and write code quickly and efficiently.

没企图 2024-11-05 18:29:52

我完全是新手,但我使用此页面上的信息来获取某个对象可用的所有方法并将它们发送到日志。

例如类别_集合:

$collection = Mage::getResourceModel('catalog/category_collection');


    Mage::log(
            "Methods for class ".get_class($collection)." ".print_r(get_class_methods($menu),true),
            null,
            'log_name.log'
            );

    $parent = get_parent_class($collection);

    while ($parent) {
        Mage::log(
            "Methods for parent ".$parent." ".print_r(get_class_methods($parent),true),
            null,
            'log_name.log'
            ); 
            $parent = get_parent_class($parent);   
    }

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:

$collection = Mage::getResourceModel('catalog/category_collection');


    Mage::log(
            "Methods for class ".get_class($collection)." ".print_r(get_class_methods($menu),true),
            null,
            'log_name.log'
            );

    $parent = get_parent_class($collection);

    while ($parent) {
        Mage::log(
            "Methods for parent ".$parent." ".print_r(get_class_methods($parent),true),
            null,
            'log_name.log'
            ); 
            $parent = get_parent_class($parent);   
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文