pTutorial

New way to learn PHP

PHP 5 this keyword

PHP this keyword

The $this is a keyword which represents the current object or current instance of class. $this is a special self-referencing variable that is used to access properties and methods from within the class.

Example of this keyword

<?php

class Student
{

Public $name;
Public $roll;

function get($a, $b)
{
$this->name= $a;

$this->roll= $b;
}

function display()
{
echo $this->name."\n";

echo $this->roll."\n";
}

}

$obj= new Student();

$obj->get("John",38);

$obj->display();


?>

Output

John 38

In the above example, name and roll are the property of the Student class and you can access these properties within the class with the help of this keyword, as shown above.

Download Source Script


QUOTES OF THE DAY

Beauty is truth, Truth beauty- That is all

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER