Sunday, 15 September 2013

oop - Why do we have to use $this-> operator? | PHP -



oop - Why do we have to use $this-> operator? | PHP -

i'm developing application in php. trying figure out utilize of $this-> , why preferred.

i mean can echo property value within method using code

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

but, in examples see $this-> used in way:

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

is utilize of $this-> preferred or first method totally normal?

$this referencing current object.

as per php.net

the pseudo-variable $this available when method called within object context. $this reference calling object (usually object method belongs, perchance object, if method called statically context of secondary object).

php oop

No comments:

Post a Comment