Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I am confused about this method declaration. In this class Wrapper.php (Mage/Page/Block/Html/) under _toHtml() method, there is a line coded as below.

$id = $this->hasElementId() ? sprintf(' id="%s"', $this->getElementId()) : '';

Which class defined this method hasElementId()? I tried to search current class and all parent class, none of them are declared this method.

Please enlighten me on this.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

No class defined that. Magento model objects and block objects all have "magic" methods implemented via PHP's __call method (defined on Varien_Object).

There are magic get, set, and has methods. That means you can set data on a block or model (or any object that inherits from Varien_Object) with the following. Follow along with the comments below

//assuming we're inside a block or model object
class Package_Module_Block_Some extends Mage_Core_Block_Abstract
{
    //...
    public function someMethod()
    {
        //if you try to get something that hasn't been set, you'll get null
        var_dump ( $this->getSomeValue() );

        //so lets set it, and try again          
        $this->setSomeValue('Foo Bar');
        var_dump( $this->getSomeValue() );

        //then there's the has method
        if($this->hasSomeValue())
        {
            var_dump( 'The data value some_value (SomeValue) exists');
        }
        else
        {
            var_dump( 'The data value some_value (SomeValue) does not exists');
        }

       if($this->hasSomeOtherValue())
       {
           var_dump( 'The data value some_value (SomeValue) exists');
       }
       else
       {
           var_dump( 'The data value some_value (SomeValue) does not exists');
       }
    }
    //...
}

So long as you don't define a method with the same name, you can use the methods prefixed with set, get, and has to set, get and check for the existence of data properties on your object in Magento.

share|improve this answer
    
@Alan.. Nice Answer and thanks :) –  MagentoBoy Jan 20 at 5:13
    
Appreciate Alan. Then how would I know all method names that created by magic methods? I assumed those properties (e.g: elementId) must be defined somewhere. –  Leongelis Jan 20 at 6:52
    
@Leongelis You don't. It depends entirely on what data has, or has not, been added to to the object. Try var_dump($this->getData()); to see all the data property, and then camel case the key names to get a list of magic methods. –  Alan Storm Jan 20 at 7:19
    
I see. Alan, you kind help really mean a lot to everyone here. Greatly appreciate. –  Leongelis Jan 20 at 7:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.