Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial - $this variable explained

Posted on : 14-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

4

$this variable is a pointer to the object making the function call. $this variable is not avialable in static methods. We will learn more about static methods in the next series of tutorials.





Example:

class Customer {
 
   private $name;
 
   public setName($name) {
	$this->name = $name;
   }
 
}
 
$c1 = new Customer();
$c2 = new Customer();
 
$c1->setName(“Sunil”);
$c2->setName(“Vishal”);

In the above example, $c1 and $c2 are two separate Customer objects. Each object has its own memory to store names. But if you see the function setName() is common. During run time, how can it make a difference as to which memory location to use to store the function values. Therefore, it uses the $this variable. As mentioned earlier, $this variable is a pointer to the object making a function call. Therefore when we execute $c1->setName(”Sunil”), $this in the setName() function is a pointer or a reference to the $c1 variable.

Related Posts on PHP5 Tutorial - Object Oriented Programming (OOPS)

  1. PHP5 Tutorial - Learn to create a PHP5 Class
  2. PHP5 Tutorial - Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial - Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial - Defining Methods of a PHP5 Class
  5. PHP5 Tutorial - Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
  8. PHP5 Tutorial - $this variable explained
  9. PHP5 Tutorial - instanceOf Operator Explained
  10. PHP5 Tutorial - Magic Methods - __toString() method
  11. PHP5 Tutorial - Magic Methods - __get() and __set()
  12. PHP5 Tutorial - Magic Methods - __isset() and __unset()
  13. PHP5 Tutorial - Magic Methods - __call() method
  14. PHP5 Tutorial - Magic Methods - __autoload() method
  15. PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
  16. PHP5 Tutorial - Magic Methods - __clone() method




Comments (4)

its very awesome,fantastic,amazing and …. and ……

nice tutorial. what is the explain of ->name? the letter after $this? and what if the function? are we can change ->name with another word? e.g. ->myname?

Thank You..

hi…i found this website quite helpful as it helped me in brushing up the basics…there was a gap of more than 8-11 months…it helped me to revise…it shud b done at bigger level….

Thank you Firoz for your comments.

Write a comment

Enter this code