PHP - SPLessons

PHP Classes and Objects

Chapter 43

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Classes and Objects

shape Introduction

The main feature of PHP is Object-Oriented Programming. It is also called an OOPS concept. OOPs were added to PHP in version 4.In version 5, the object model was re-modeled with better performance. The basic building blocks of object-oriented programming are “Classes and Objects”.

A single PHP program can contain any number of classes. They are the user-defined types that consist of objects and is the combination of data and functions. These two are called as members of the class.

Class is the blue-print of an object.It does not define any data directly but the class represents two important functions of an object such as:

A class is represented with a keyword class The identifier used specifies the class name. Class body is given in {}. The class definition ends with a semicolon ;.

<?php
class class_name
{
data statements;
function statements;
};
?>

PHP Objects

shape Description

Objects of a class are similar to the variables of a basic PHP program. They represent the divisions of a class. The below diagram shows “Classes and Objects” related to the real world.

Below is the syntax to represent an Object.An object of the class can be created by using new keyword.

$objClass = new myClass();

Multiple objects of same class can also be created in which each object is different from other.
[php]
$objClass1 = new myClass();
$objClass2 = new myClass();[/php]

Object Operator : To access object property, object operator is used.This is denoted with ->.

shape Figure

shape Example


[php]
<!DOCTYPE html>
<html>
<body>
<?php
//Creating class interestCalculator
class interestCalculator
{
public $rate;
public $duration;
public $capital;
public function calculateInterest()
{
return ($this->rate*$this->duration*$this->capital)/100;
}
}

//Creating various object of class interestCalculator to calculate interest on various amount
$calculator1 = new InterestCalculator();
$calculator2 = new InterestCalculator();

//Accessing object properties
$calculator1->rate = 3;
$calculator1->duration =2;
$calculator1->capital = 300;

$calculator2->rate = 3.2;
$calculator2->duration =3;
$calculator2->capital = 400;

$interest1 = $calculator1->calculateInterest();
$interest2 = $calculator2->calculateInterest();

echo "Your interest on capital $calculator1->capital with rate $calculator1->rate for duration $calculator1->duration is $interest1 <br/> ";
echo "Your interest on capital $calculator2->capital with rate $calculator2->rate for duration $calculator2->duration is $interest2 <br/> ";
?>
</body>
</html>
[/php]

Output:
[php]Your interest on capital 300 with rate 3 for duration 2 is 18
Your interest on capital 400 with rate 3.2 for duration 3 is 38.4 [/php]

Constructors

shape Description

As soon as the class with objects is executed, a member function called “Constructor” is generated.The name of constructor was same as the name of class until version 4.But from version 5, it was replaced with function __construct(it has two underscores).

[php]
<?php
class interestCalculator
{
public $rate;
public $duration;
public $capital;

//Constructor of the class
public function __construct($rate , $duration)
{
$this->rate = $rate;
$this->duration = $duration;
}
}
$objCls = new interestCalculator(3.2 , 7) //passing value of $rate and $duration
?>
[/php]

Access Modifiers

shape Description

The main aim of Object Oriented Programming is to hide the data and provide security to the application users. Modifiers of classes are used to achieved them as they restrict the hackers without providing the access to the class members and data present in it. So, modifiers also called as “ Access Specifiers“. Three access specifiers are available. They are:

Public

The member of a class with a Private Access Specifier is not accessible and viewed outside of the class. The variables declared with a private keyword can be retrieved and accessed only by class and neighbor functions. By default, all the members of the class are declared as private.

Private

The member of a class with Public Access Specifier is accessed anywhere outside the class but only inside the program. The variables declared with a public keyword can be retrieved and accessed without member function.

Protected

The member of a class with Protected Access Specifier is not accessible and viewed outside the class same as Private Access Specifier. But, the advancement in protected is, the child class members can access it otherwise called as Derived Classes. It is a subclass of a class.

[php]
<?php
class person {
var $name;
public $height;
protected $social_insurance;
private $pinn_number;

function __construct($persons_name) {
$this->name = $persons_name;
}

function set_name($new_name) {
$this->name = $new_name;
}

function get_name() {
return $this->name;
}

}
?>
[/php]

Summary

shape Points

  • Object is an instance of class.
  • Class is blue-print of an object having all its variables and methods.
  • Modifiers also called as Access Specifiers (Public, Private and Protected)
  • When Class with objects is executed, a member function called Constructor generates.


PHP – Related Information
PHP Installation
PHP Syntax
PHP Data Types
PHP Operators
PHP Arrays

Click Here to Join – Telegram Channel