This page looks best with JavaScript enabled

Object Oriented PHP

 ·  ☕ 3 min read  ·  🤖 Arjit Sharma

Class Creation

Below is given basic class implementation in PHP
__CLASS__ constant returns name of class
Line 8: Constructor Defined with default values set to null
Line 16: Destructor is called at end of the script
Line 30: Object’s properties can be accessed using -> keyword

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Student {
//className = __CLASS__;
public $name;
public $roll;
public $class;

//Constructor
function __construct($name=null,$roll=null,$class=null){
    //default set to null so that no error occurs even if user doesn't set all values
    $this->name =$name;
    $this->roll =$roll;
    $this->class =$class;
}

//Destructor
function __destruct(){
echo "Called upon script ending";}

//Method
public function sayHello(){
    return "Hello World!!";
    }
}

//Instantiating Objects
$arjit=new Student("Arjit Sharma",14,"CSE-A");


//Getting & Setting Object's Properties
echo $arjit->roll; //Property does'nt start with $ sign
$arjit->roll="CSE-B";
echo $arjit->sayHello();

Getters and Setters

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class User{
private $name;

public function__construct($name){
$this->name=name;
}

public function __get($property){
	if(property_exists($this,$property)){
	$this->property=$value;
	}
return $this;
}

public function __set($property, $value){
	if(property_exists($this,$property)){
	return $this->property;
	}
}

$user1 = new User('Arjit');
echo $user1->__get('name');
$user1->__set('name',"Arjit Sharma");

Constants and Static

Though both are shared by all objects(are class variable)
A constant(const) cannot be changed while a static variable can.

1
2
3
4
5
6
7
8
class Student {
const a="5";
static $b="5";
}
echo Student::a;//5
echo Student::$b;//5
Student::a=10;//Error
Student::$b=10;//Changed to 10	

Inheritance

extends keyword is used.
Line 13:Child constructor used to call parent constructor.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Student{
public $name;

public function __construct($name){
    $this->name=$name;
    }
}
class CollegeStudent extends Student{
public $degree;

public function __construct($name,$degree){
    // $this->name=$name; --->this would have worked here
    Student::__construct($name);//Calling parent constructor
    $this->degree=$degree;
    }
}

$student1= new CollegeStudent("Arjit Sharma","B.Tech");
echo "Name : ".$student1->name." Course : ".$student1->degree;

Traits

As inheritance only allows to inherit from 1 class, What if we wanted a particular function in all classes. Use trait.
Traits are used to declare methods that can be used in multiple classes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
trait msg{
public function msg_function(){
    echo "Hello World!";
    }
}
class Welcome{
use msg;
}
$ob=new Welcome();
$ob->msg_function();

Method Overriding

If we don’t want a method to be overridden, prefix with final keyword. (Ex: final public function sayHello())

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class A{
public function sayHello(){ //Prefix with final keyword so that cant be overridden
    return "Hello from A";
    }
}

class B extends A{
public function sayHello(){
    return "Hello from B";
    }
} 

Abstract Classes

abstract keyword is used. A class can only inherit from 1 class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
abstract class human{

public function walk(){//Can have non-abstract mehods
    return "Human is walking";
    }
abstract public function sayHello();//Abstract method declare
}

class Indian extends human{

public function sayHello(){//Indian define there implementation of saying hello
    return "Namaste";
    }
}

Interfaces in PHP

implements keyword is used. A class can implement multiple interfaces.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
interface A { 
function A_method();
}

interface B { 
function B_method();
}

class C implements A,B {

function A_method(){
    return "A's Method";
}

function B_method(){
    return "B's Method";
    } 
}
Share on

Arjit Sharma
WRITTEN BY
Arjit Sharma
Yo, I am a CS enthusiast or am I ? Just kidding.


What's on this Page