What does $this-> mean?

Hi all

I know a bit of PHP simple if else etc amongst other things… but I cant seem to get my head around what $this-> means and what -> means on its own?

Example, I’m working with Joomla, Magento and seeing lots of the below in the templates, I need to get a understanding so i can start changes things:

<?php if($this->countModules('left')) : ?>
<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>

from another forum:

If using wrapper classes, you need to use $this-> to access the variables in the base class.
Can somebody explain in English, is it some sort of echo? just trying to understand :slight_smile:

Thanks :cool:

When you create an instance of an object, you use $this->propery and $this->method() to access properties and methods of that instance.

For example:

class Person
{
    protected $_name;
    protected $_lastname;
    
    public function __construct ($name, $lastname)
    {
        $this->_name = $name;
        $this->_lastname = $lastname;
    }

    public function getName ()
    {
        return $this->_name;
    }

    public function getFullName ()
    {
        return $this->getName() . ' ' . $this->_lastname;
    }
}

// Create an instance of Person
$person = new Person('John', 'Doe');
// Get the name and full name of that object
echo 'My first name is: ', $person->getName(), "<br/>";
echo 'My full name is: ', $person->getFullName();

The $this keyword in the HTML templates in Joomla and so on, refers to an instance of the Template class that handles all the assigned variables and functions used to fill in the templates.

Classes have methods, that like functions can take arguments. The “this” is like a “magic word” that means “this class” and the “->” connects to the class’ method eg. countModules() where the argument is “left”

In the case of if( class::method() ) the method will either return a FALSE or something else, a simple boolean conditional test. In the case of echo class::method() it will echo whatever is returned.

More clear or muddier?

Think of $this-> to mean “in this copy of this object, use…”

In the example above, $this->_firstname would mean "in this copy of this object, use the variable “_firstname”

<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>

$this is how an object refers to its self. It’s a bit like saying “me”.
getBodyClass() is a method (function) of that object. In this case it’s called without arguments and will return a string class name, or null, false or empty string if none is applicable (I can’t tell which from looking at this code)
The ? is the terniary operator. It’s a short cut for using if, else logic. (see below)

So if getBodyClass() returns the string product The script will echo class=“product”, if getBodyClass() returns nothing then it’ll echo nothing and not alter the HTML class

The same thing could be written like this:


<?php
if($this->getBodyClass()) {
    echo ' class="' . $this->getBodyClass() . '"';
    
    //example
    // class="product"
}

Terniary Example


$message = (is_today_friday()) ? "Happy Friday" : "It's not friday";

/* $message will be Happy Friday if is_today_friday() returns true, or It's not friday otherwise */

//Exactly the same as
if(is_today_friday()) {
    $message = "Happy Friday";
} else {
     $message = "It's not friday";
}

Cheers Guys

You make it look so easy hexburner :slight_smile: I’ll have to read up on instance, object, methods… got some notes somewhere need to refresh my brain on what all this means (:

Thanks Mittineague: muddier :confused:

Cheers Force Flow, I kind of understand but nothing clicks when I look at the code, if you get what I mean.

A few more:

echo $this->getChildHtml('header')

$store_cats = $obj->getStoreCategories();

echo $obj->drawItem($_category);

Cheers cranial-bore just reading your post, posted the same time and thanks for the Terniary Example, making a bit more sense, I’ll have to have a read and digest and get back, thanks again :cool:

It’s not that hard to learn about this.
Some programmer call it a ‘bonus’ parameter that every object has

It gets a lot more confusing in JavaScript, where this is unpredictable and can actually refer to different objects, depending on how and when you execute the same script.