0

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;
?>
6
Contributors
6
Replies
22
Views
3 Years
Discussion Span
Last Post by pelle12
0

$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

1

$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.

0

Hi diafol,

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

Regards,
JP

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.