PHP OOPs tutorial Part1

 OOP's Basics in PHP 

Part2➡

coding-zon-php-oops-part1

Table of Contents

Basics of Class in PHP

Constructors and Destructors

Object-Oriented Programming(Part-2)

  • Polymorphism
  • Inheritance
  • Function Overriding
  • Parent Keyword
  • Static Keyword
  • Interface Class
  • Abstract Class

 

 

OOPs Introduction

 In programming, there are two approaches. 

  1.  Procedural
  2.  Object-oriented

There are more advantages in  Object-Oriented Programming in terms of security and reusability etc.

Go ↑ 

Feature of OOPs


 In every Object-Oriented Programming Languages, we will find these four Oops features. They are as follows.

  1.  Abstraction: Hiding the complexity. User need to know how to use it, not how it is implemented.
  2.  Encapsulation: Binding data and functions together in a single entity(class) is known as encapsulation.
  3.  Polymorphism: It means, many forms. Ex: overloading, overriding comes under polymorphism.
  4.  Inheritance: Reusability of existing code.


What is  Class in PHP OOPs?

In Object-Oriented Programming, Class is the basic element. It is an entity and all OOP concepts are implemented in classes. Class defines the structure of an object. Class is conceptual. Using class, we create an Object.
In this tutorial, we will be discussing on declaring class in PHP.

Defining a Class in PHP:

  • A Class starts with a keyword  “class”, followed by class name.
  • A Class name First letter is uppercase.
  • A class contains member variables, member functions.
  • All the logic implemented inside the member functions.


The basic declaration of a class:

<?php

class Person
{
public function f1()
{
echo "Hello";
}
}

?>
 

Go Top↑ 

Creating a Class Objectin PHP

To make use of a class. An object need to be created using a class. This is known  as instantiating. It can be done using new keyword. 

 
Syn1:
$objectName = new Classname(); 



objectName: 

 --- It is a new object name that you want to create. Object name starts with,

$ same as variable. 

Classname:

 ---- The class name that you are using to create an object. Parenthesis '()' are

optional. The above syntax can be written as follows.

 
Syn2:
$objectName = new Classname; // without parenthesis 
 
Example:
 

 

 
 
 
coding-zon-php-obj-instantiation

 
 
The above class can rewrite as show below.
 
<?php

class Person
{
public function f1()
{
echo "Hi there";
}
}

$obj1 = new Person ();  //person instance is created, obj1 is called instance

                                      // $obj1 will point to the object of Person

$obj1->f1();                  // invoking instance function

?> 
 
output:
Hi there 
 

One object-reference can be assigned to another object-reference.

 

coding-zon-php-object-multi-reference

 

 
Example:

 
 

$obj2 = new Person();
$obj3 = new Person();
 
$obj2->f1();
 
//one object-reference can be assigned to another obj-ref
 
$obj3 = $obj2;  
 
$obj3->f1(); 
 
 

An instance can be pointed by multiple reference. In the above,  $obj2, $obj3 pointing to the same instance.

  
 
 

 

 Unsetting/removing  an object reference

 
 
 unset($obj2);
 

 

Difference between Class and Object

One class can have the multiple number of instances. (There can be a number of instances of the class “Person”, like “p1”, “p2” etc.).


$p1= new Person();
$p2= new Person();

MoveTop↑ 

Define Attributes for Class in PHP

What are Attributes for Class?

Attributes, or the data members, created inside the Class.
In Object oriented programming, attributes are members of the class. Attributes hold the data required for the class to perform its functionality. Attributes also known as member variables. These are instance variables.

 

Example:

 
<?php
 
class Student
{
  public $sname;
  public $rollno;
 
  public function setName($name)
  {
    $this->sname=$name;
  }
  public function getName()
  {
    echo $this->sname;
  }
}
 
$stu1 = new Student ();
 
$stu1->setName("John");
$stu1->getName();
 
?>

Attributes declared along with Access Specifiers.
Access Specifiers
data member's scope is decided by access specifiers, public, private and protected.
The default Access Specifier for an attribute is public.
Private – can only be accessed inside the class. So outside the class
cannot access private attributes.
Protected – can only be accessed by the derived classes while performing
inheritance.
Public – can be accessed internally and by outside world.

 


<?php
 
class newClass
{

  public $first_name;            //available to other classes

  private $second_name;     //available to within the class functions

  protected $third_name;     //available to childclasses 

 
}

?>
 

MoveTop↑ 

Define Methods for the Class in PHP OOPs

What are Methods in Class?

  • Methods will implement the functionality for the class.
  • Method performed on the data. Methods can communicate with each other by means of message passing.
  • Methods can be declared as Private, Protected or Public Access Specifiers.

Access specifiers.

Private – These methods can only be accessed within the class. Outside world cannot access these methods
Protected – Protected methods can only be accessed by the derived classes while performing Inheritance, but the outside world has no access to these methods.
Public – These methods can be accessed internally and also by outside world.

 

Example:

 
<?php
class Customer
{
public $cname;
 
public function setName($name)
{
    $this->cname=$name;
    $this->displayName();
}
 
private function getName()
{
    echo $this->cname; //Access Method within the class
}
}
    $a = new Customer();
    $a->setName("John"); //Access Method outside the class
 
?>

In above Customer class is having methods setName() and getName().
Now, to access these methods within the class, $this operator is used.
To access these methods from outside the class, the object variable is used .

Defining Constants in PHP

In PHP by default all attributes that we define are instance or object variables.  you can define class level variables known as constants. They are read only variables, will not change its value within the class scope.

  • Constants declared using “const“.
  • Only a string or numeric value can be assigned to a constant.
  • Arrays, Objects, and expressions cannot be assigned to a constant.
  • A class constant can be accessed via the scope resolution operator (::) along with class name.

Example :

<?php
 
class Voter
{
const city = "XYZ city"; //stores some fixed values.
}
echo " City is : " . Voter::city;
 
?>

 Output: City is : XYZ city
.

Construct() Method in PHP

  • Constructors are a special function in Object-Oriented Programming.
  • It gets called immediately after a new object is created.
  • This is useful for performing any pre operation before we start to call the methods of the class.
  • Construct function starts with two underscores (__)
  • PHP uses a special keyword "__construct"  to define the constructor of the class.


Example :

 
<?php

 

class employee
{
  function __construct()
  {
    echo "<br>Constructor Called";
  }
}
 
$a = new employee(); //() are optional
$b = new employee;
 
?>
 
output:
Constructor Called
Constructor Called 

 When an employee object created,  constructor automatically gets called. 

Example 2:

<?php
 
class employee
{
  function __construct($n)
  {
    echo "<br>Your ID is ".$n;
  }
}
 

 $a = new employee(10);        //passing parameter to constructor. 

 $b = new employee(20);        //passing parameter to constructor.

 

?>
 
 
output:
Your ID is 10
Your ID is 20

 
 

GoTo Top↑ 

Destruct() Method

  • Destructors  are a special function in Object-Oriented Programming.
  • It gets called automatically whenever an object of a class is destroyed.
  • This is useful for  cleaning the memory,  which increments the application performance.
  • PHP  uses the special keyword “__destruct” to define the destructor of the class.So when an object finishes the operation, the destructor function is called automatically.

 

NOTE:
Destructor, always  defined as a public.

 

 Example on Destructor in PHP:

 

<?php
 
class employee
{
function __construct()
{
    print " <br>"Constructor called";
    
}
function __destruct()
{
    echo "<br>"Destructor Called";
}
}
 
$a = new employee();
 
?>

 output:

Constructor called
Destructor Called

 

 

To Run the code, You can use any  online compiler:

Example: Compiler


 


 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP