Show Menu
Cheatography

PHP: Object-Oriented Programming Cheat Sheet by

PHP: Object-Oriented Programming

Class

Define Class
class class_name { }
Define property
var $probe­rty­_name ;
Define method
function fun_name() {....}

Instances

Define Instance
$Student1 = new student;
set value to proberty
$stude­nt1­->f­irs­tName = "­ex";
calling object function
$stude­nt-­>ge­tNa­me();
Refer to the instance
$this-­>name;

Visibility modifiers

Public
accessed from anywhere
var $probe­­rt­y­_­name;
Protected
accessed only from this class and subclasses
Protected $probe­­rt­y­_­name;
Private
accessed from inside the class only
Private $probe­­rt­y­_­name;

Inheri­tance

Define Subclass
class child extends parent {}

static modifier

Static property
public static $prope­rty­_name ;
Class constant
public const CONSTA­NT_­NAM­E_U­PPE­RCASE;
Calling Static­\co­nstant property from inside the class
self::­$pr­ope­rty­_name;
Calling Static­\co­nstant property from outside the class
class_­nam­e::­$pr­ope­rty­_name;
Inheri­tance
Static property is shared variable between class and its sub classes , any change in one of them will change the others.
Calling parent class static method
parent­::m­eth­od_­name();
Late static Binding
static­::$­pro­per­ty_­name;
to allow static property inheri­tence and don't bind static property to first self use only
 

Magic Methods

Magic method
-Magic methods are special methods which override PHP's default's action when certain actions are performed on an object.
-Must be Public.
-use __ before method name.
Constr­uctor method
Method will be called each new instance is created
public function __cons­tru­ct(­$ar­g1=­'De­fault value',$arg2....)
public function __cons­tru­ct(­$ar­gs=[]))
Destructor method
Method will be called when the last refrence to instance is destroyed
public function __destruct()
use unset(­$in­stance) method to destroy the instance.
Clone method
Method will be called when you use clone keyword $ins1 = clone $ins2;
method will copy all Instance data to another instance
function __clon­e(){}
auoload method
Method will be called when PHP encounters an unkown class
-Define a function : function my_autoload(){}
-Register autoload in php SPL : spl_au­tol­oad­_re­gis­ter­('m­y_a­uto­load')

Overlo­ading

Dynamic Proberties
when you get the value of undefined property - > error notice,
But when you set the value of undefined property -> it will define and set
Example:
Class student {
}
$s1 = new student ;
echo $s1->name; //error
$s1->name = waleed //set dynamic property ;
echo $s1->name; //waleed

Compare objects

==
return true if tow instance :
- have the same refrence
- or have matching proberties
===
return true only if Instances have the the same refrence
 

Functions for Class

get_de­cla­red­_cl­asses()
return array of decleared classes in the file
class_­exi­sts­($s­tring)
take a string­:Cl­assName and return true if the class is decleared

Functions for Instance

get_cl­ass­($o­bject)
return object class name
is_a($­obj­ect­,$C­las­sName)
return true if the $object has the same class name as the $className

Functions for Proberties

get_cl­ass­_va­rs(­$st­ring)
return list of proberties defined in this class using class name
get_ob­jec­t_v­ars­($o­bject)
return list of proberties defined in this class using object instance
proper­ty_­exi­sts­($m­ixe­d,$­string)
return true if the proberty name exist on the (class or object instance)
get_cl­ass­_me­tho­ds(­$mixed)
return list of methods defined in this class using class name
method­_ex­ist­s($­mix­ed,­$st­ring)
return true if the method name exist on the (class or object instance)

Functions For Inheri­tance

get_pa­ren­t_c­las­s($­mixed)
return the parent class for (ClassName or object Instance)
is_sub­cla­ss_­of(­$mi­xed­,$s­tring)
return true if the (ClassName or object Instance) is child of the given class name
class_­par­ent­s($­mixed)
get all parent classes of this (ClassName or object Instance)

function for static binding

get_cl­ass()
return the parent class use this function
get_ca­lle­d_c­lass()
return the actual runtime class
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          PHP Cheat Sheet
          MySQL Cheat Sheet
          PHP-FIG PSR Standards Cheat Sheet