dear all, i have a code in php class. through that code plz help me to solve my problem. in the below code why we use $this->message .. can't we use $message under constructor? plz pl ztell me
thanks

myClass.php

<?php
class myClass
{
     public $message;

     public function __construct()
     {
          $this->message  = 'Hello, World!' ;
     } 

     public function echoMessage()
     {
          echo $this->message;
     }
}

$obj = new myClass;
echo $obj->message;
?>

$this is used to refer the envoking object inside the class definition.
every envoked object would have seperate copy of data members, in order to refer to the envoking object's data member you specify $this

Member Avatar

diafol

$this refers to a property or method of the object itself. The class may use other trivial variables for calculating intermediate values, for string manipulation etc, but they would not be 'properties' of the entity (class). Those trivial variables would not be considered useful to other methods within the class or to subclasses or to the client.

Hi diafol,

Yes you are right it just refers to a property or a method of an object itself.

Regards,
JP