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

obj1->echoMessage();

whats this?

Member Avatar for 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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.