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)

Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top