site-icon

Arjit Sharma


Object Oriented PHP

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

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

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.

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.

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.

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())

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 Class

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

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

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

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";
    } 
}