Cheatography
https://cheatography.com
PHP: Object-Oriented Programming
Class
Define Class |
class class_name { } |
Define property |
var $proberty_name ; |
Define method |
function fun_name() {....} |
Instances
Define Instance |
$Student1 = new student; |
set value to proberty |
$student1->firstName = "ex"; |
calling object function |
$student->getName(); |
Refer to the instance |
$this->name; |
Visibility modifiers
Public |
accessed from anywhere |
var $proberty_name; |
Protected |
accessed only from this class and subclasses |
Protected $proberty_name; |
Private |
accessed from inside the class only |
Private $proberty_name; |
Inheritance
Define Subclass |
class child extends parent {} |
static modifier
Static property |
public static $property_name ; |
Class constant |
public const CONSTANT_NAME_UPPERCASE; |
Calling Static\constant property from inside the class |
self::$property_name; |
Calling Static\constant property from outside the class |
class_name::$property_name; |
Inheritance |
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::method_name(); |
Late static Binding |
static::$property_name; to allow static property inheritence 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. |
Constructor method |
Method will be called each new instance is created public function __construct($arg1='Default value',$arg2....) public function __construct($args=[])) |
Destructor method |
Method will be called when the last refrence to instance is destroyed public function __destruct() use unset($instance) 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 __clone(){} |
auoload method |
Method will be called when PHP encounters an unkown class -Define a function : function my_autoload(){} -Register autoload in php SPL : spl_autoload_register('my_autoload') |
Overloading
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_declared_classes() |
return array of decleared classes in the file |
class_exists($string) |
take a string:ClassName and return true if the class is decleared |
Functions for Instance
get_class($object) |
return object class name |
is_a($object,$ClassName) |
return true if the $object has the same class name as the $className |
Functions for Proberties
get_class_vars($string) |
return list of proberties defined in this class using class name |
get_object_vars($object) |
return list of proberties defined in this class using object instance |
property_exists($mixed,$string) |
return true if the proberty name exist on the (class or object instance) |
get_class_methods($mixed) |
return list of methods defined in this class using class name |
method_exists($mixed,$string) |
return true if the method name exist on the (class or object instance) |
Functions For Inheritance
get_parent_class($mixed) |
return the parent class for (ClassName or object Instance) |
is_subclass_of($mixed,$string) |
return true if the (ClassName or object Instance) is child of the given class name |
class_parents($mixed) |
get all parent classes of this (ClassName or object Instance) |
function for static binding
get_class() |
return the parent class use this function |
get_called_class() |
return the actual runtime class |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets