Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I learn the methods that can be accessed for certain objects?

Tags:

magento

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?

like image 466
Chris Avatar asked Apr 01 '11 18:04

Chris


People also ask

How do you access the methods of an object?

To access the object, a method can use the this keyword. The value of this is the object “before dot”, the one used to call the method. Here during the execution of user. sayHi() , the value of this will be user .

What are the methods of an object?

a method is an action which an object is able to perform. sending a message to an object means asking the object to execute or invoke one of its methods.

How do you call methods of objects?

Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.

How can we make methods available on all objects?

How can we make methods available on all objects? Explanation: It is possible to add methods to Object. prototype, making them available on all objects. This is not recommended, however, because prior to ECMAScript5, there is no way to make these add-on methods non enumerable, and if you add properties to Object.


1 Answers

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.

like image 142
Alan Storm Avatar answered Dec 02 '22 13:12

Alan Storm