Classes and Objects – PHP OOP

A class is a template for objects, and an instance of a class is an object.

OOP Case

Let’s say our class is called Student. A Student can have a name, rollno,  dob, and so on. The values of these properties can be stored in variables like $name, $rollno, and $dob.

When the individual objects (Ram, Shyam, etc.) are made, they inherit all of the class’s properties and behaviors, but each object’s properties will have different values.

Define a Class

Use the class keyword, then the name of the class, and a pair of curly braces () to define a class. The braces hold all of its properties and methods:

Syntax

<?php
class Student{
// code goes here…
}
?>

Below, we make a class called Student. It has two properties ($name and $rollno) and two methods for setting and getting the $name property: set name() and get name().

<?php
class Student{
// Properties
public $name;
public $rollno;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>

The variables in a class are called “properties,” and the functions are called “methods.”

Define Objects

Objects are what make classes work. From a class, we can make more than one object. Each object has all the properties and methods that the class defines, but the values of the properties will be different.

The new keyword is used to make objects of a class.

$name1 and $name2 are examples of the class Student in the code below:

Example

<!DOCTYPE html>
<html>
<body>

<?php
class Student {
// Properties
public $name;
public $rollno;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}

$ram = new Student();
$shyam = new Student();
$ram->set_name(‘Ram’);
$shyam->set_name(‘Shyam’);

echo $ram->get_name();
echo “<br>”;
echo $shyam->get_name();
?>

</body>
</html>

Output

Ram
Shyam

In the example below, we add two more methods to class Student, for setting and getting the $rollno  property:

Example

<!DOCTYPE html>
<html>
<body>

<?php
class Student {
// Properties
public $name;
public $rollno;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_rollno($rollno) {
$this->rollno = $rollno;
}
function get_rollno() {
return $this->rollno;
}
}

$ram = new Student();
$ram->set_name(‘Ram’);
$ram->set_rollno(‘100’);
echo “Name: ” . $ram->get_name();
echo “<br>”;
echo “Rollno: ” . $ram->get_rollno();
?>

</body>
</html>

Output

Name: Ram
Rollno: 100

The $this Keyword in PHP

The $this keyword talks about the current object and can only be used in methods.

Take a look at the following:

<?php
class Student{
public $name;
}
$ram = new Student();
?>

So, where do we change the $name property’s value? The two ways are:

1. inside the class(Adding a set name() method and using $this):

Example

 

<!DOCTYPE html>
<html>
<body>

<?php
class Student{
public $name;
function set_name($name) {
$this->name = $name;
}
}
$ram= new Student();
$ram->set_name(“Ram”);

echo $ram->name;
?>

</body>
</html>

Output

Ram

 

2. Outside the class (by changing the value of a property directly):

Example

<!DOCTYPE html>
<html>
<body>

<?php
class Student{
public $name;
}
$ram= new Student();
$ram->name = “Ram”;

echo $ram->name;
?>

</body>
</html>

Output

Ram

PHP –  instanceof

You can use the keyword instanceof to see if an object belongs to a certain class:

Example

<!DOCTYPE html>
<html>
<body>

<?php
class Student {
// Properties
public $name;
public $color;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}

$ram = new Student();
var_dump($ram instanceof Student);
?>

</body>
</html>

Output

bool(true)

People also search

Leave a Comment

Scroll to Top