2

I'm developing an application in PHP. I am trying to figure out the use of $this-> and why it is always preferred.

I mean we can simply echo a property value inside a method using this code

<?php
class place{
    public $country;
    public function countryName($country){
        echo $country;
    }
}
$info = new place();
$info->countryName("Nepal");
?>

But, in examples I see that $this-> is used in this way:

<?php
class place{
    public $country;
    public function countryName($country){
        $this->country = $country;
        echo $this->country;
    }
}
$info = new place();
$info->countryName("Nepal");
?>

Is the use of $this-> preferred or is the first method totally normal?

Daryl Bennett
  • 462
  • 10
  • 22
Ashish Singh
  • 135
  • 1
  • 14

7 Answers7

5

$this is referencing to the current object.

As per php.net

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

fortune
  • 3,361
  • 1
  • 20
  • 30
4
$this->country 

is country relative to the class

$country 

is relative to the method

Taku
  • 5,639
  • 2
  • 42
  • 31
2

$this->country will echo out your class level $country whereas just echo $country will echo out your method level $country. This is all because of how objects work in PHP and the scope of variables. As you keep looking you'll see the use of this a lot more

DrRoach
  • 1,320
  • 9
  • 16
2

The first arc isn't echoing a property it's just echoing the value that is passed in.

The second arc assigns the passed in value to the property, and then you use $this->country to echo the property.

If you echo $this->country in the first arc, you'll get nothing echo'd.

Cliffordlife
  • 488
  • 5
  • 13
2
$this-> 

Helps you refer to your class variables.

for example:

   public function countryName($country){
         $this->country = $country;
        echo $this->country;
    }

Your $this->country is referring to the class var, and it's needed to be set to the parameter $country.

Daryl Bennett
  • 462
  • 10
  • 22
  • Pardon me, it would appear that I'm mistaken. You can code without it in Java and other OOP, but it looks like in PHP you need to use `$this` http://stackoverflow.com/questions/4353970/this-keyword-in-java-and-in-php – Daryl Bennett Nov 14 '14 at 17:38
2

$this represents any instance of the class. So when you create an object $USA and call countryName($country), $this will represent the object $USA:

<?php
$USA = new place();
$USA->countryName("USA");
?>

In your code, you are echoing the parameter of the function countryName($country) but not the class attribute, but here:

<?php
class place{
    public $country;//This is the class attribute.
    public function countryName($country){
        $this->country = $country;/*here you are storing the value of the parameter passed to the function into the class attribute ($this->country)*/
        echo $this->country;
    }
}
?>
onlyforthis
  • 444
  • 1
  • 5
  • 21
1

The advantage to using $this->country versus $country, is that you can use $this->country in other places as well. The value is stored in the object $info.

For example:

// #1 case
class place {
  public $country; // This is not used
  public function countryName($country) {
    echo $country;
  }
}

$info = new place();
$info->countryName("Nepal");
// Result:
//   Nepal


// #2 case: Using $this
class place {
  public $country; // This is not used
  public function countryName($country) {
    $this->country = $country;
    echo $this->country;
  }
  public function whereAmI() { // NO parameters
    echo 'You are in the country "' . $this->country . '"';
  }
}

$info = new place();
$info->countryName("Nepal");
$info->whereAmI();
// Result:
//   Nepal
//   You are in the country "Nepal"
Chris Happy
  • 7,088
  • 2
  • 22
  • 49