Class:
1. Class is a collection of object.
2. Class is defines properties/functions of an Object.
3. Class is consist of properties and Methods.
<?php
class JackSiva
{
$properties_s = "10";
public function(){
echo "my name is Siva S";
}
}
?> |
Object:
1. Object is instance of class
2. We can create any number of object to the Class.
3. Create php Object we have to use "new" Operator
$object = new JackSiva(); //output my name is Siva S
|
Example: Reusable code
<?php
class Byke {
var $price;
var $name;
function setPrice($bykcost){
$this->price = $bykcost;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setName($byk){
$this->name = $byk;
}
function getName(){
echo $this->name ." <br/>";
}
}
//We can create any number of object to the Class.
$Hero= new Byke();
$Hero->setName("Hero");
$Hero->setPrice("60000");
$output1_1 = getName(); // Hero
$output1_2 = getPrice(); // 60000
$Bajaj= new Byke();
$Bajaj->setName("Bajaj");
$Bajaj->setPrice("80000");
$output2_1 = getName(); // Bajaj
$output2_2 = getPrice(); // 80000
$Yamaha= new Byke();
$Yamaha->setName("Yamaha");
$Yamaha->setPrice("90000");
$output3_1 = getName(); // Yamaha
$output3_2 = getPrice(); // 90000
//...
//...etc,..
?>
|
Variable Scope/ Visibility/Access Modifier:
PHP Variables Scope
Variable scope is known as its boundary within which it can be visible or accessed from code
- Local
- Global
- Static
Global:
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?> |
Global keyword
The global
keyword is used to access a global variable from within a function.
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15?> |
Local:
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function
<?php
function myTest() {
$x = 5; // local scope echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an errorecho "<p>Variable x outside function is: $x</p>";
?>
|
Static:
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static
keyword when you first declare the variable
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
|
Basic OOPS Visibility PHP
Each method and property has its visibility. There are three types of visibility in PHP. They are declared by keywords public, protected and private. Each one of them controls how a method/property can be accessed by outsiders.
Public:
It allows anyone from outside access its method/property. This is the default visibility in PHP class when no keywords are prefixed to a method/property.
Protected:
It only allows itself or children classes to access its method/property.
Private:
It does not allow anyone except itself to access its method/property.
PHP keywords as Access control Modifier
- Public
- Private
- Protected
- abstract
- final
- static - Special php keyword, we used this keyword before or after the method's visibility. like static private function abc(); or private static function abc();
Public:
class or its members defined with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.
Class : No need to mentioned public keyword in front of the class.
<?php
public class ParseError{
// Getting parse error
}?>
|
Default the class treated as a public.
<?php
class WorkingFine{
// default the class treated as a public
}
?>
|
function: Similarly, function also treated public, But we need to mentioned the public keyword in front of the function is the best practice.
<?php
class WorkingFine{
public function BestPractice(){
echo "we have to mentioned public keyword in front of the class function";
}
}
?>
|
variable/properties: Unlike class and function, we should mention the access modifier in front of the variable otherwise we are getting parse error.
<?php
class WorkingFine{
public $must = array('must','mentioned', 'public', 'keyword');
public function BestPractice(){
echo "we have to mentioned public keyword in front of the class function";
}
}
?>
|
Private:
Class members with this keyword will be accessed within the class itself. It protects members from outside class access with the reference of the class instance.
class: We can't able to use private keyword for class.
function/properties: We can use this keyword only for class variables and function.
Example:
- In this example, one private variable - You can instance the private variable via Public function.
- In this example, one private function - you can't able instance the class Private function.
- In this example, one Public function - You can instance the function.
<?php
class WorkingFine{
Private $privatevar = "Private variable only access inside the class";
public function ViaprivateAccess(){
return $this->OnlyInside(); //called private function here
}
private function OnlyInside(){
return $this->privatevar; //called private variable here
}
}
$obj = new WorkingFine();
var_dump($obj->ViaprivateAccess());
//instance of private variable/function via public class function
//print_r($obj->OnlyInside()); //invalid access private function
//print_r($obj->privatevar); //invalid access private property
?>
|
Protected:
Same as private, except by allowing sub classes to access protected super class members.
class: We can't able to use Protected keyword for class.
function/properties: We can use this keyword only for class variables and functions.
- We are used the Protected keyword to the php inheritance.
- It is prevent the access to the classes and its member from anywhere.
- Only you can access the members inside the class itself and sub classes.
Example:
<?php
class WorkingFine{
protected $countries= array("india","Unitedstate");
protected $states= array(
array("name"=>"Tamilnadu","country"=>"india"),
array("name"=>"Karnataka","country"=>"india"),
array("name"=>"kerala","country"=>"india"),
array("name"=>"Andhra","country"=>"india"),
array("name"=>"Pudhucheri","country"=>"india"),
array("name"=>"Newyork","country"=>"Unitedstate"),
array("name"=>"Los Angeles","country"=>"Unitedstate"));
protected function getState() {
for($i=0;$i<count($this->states);$i++) {
$states_list[] = $this->states[$i]["name"];
}
return $states_list;
}
protected function getStatesByCountry($country) {
for($i=0;$i<count($this->states);$i++) {
if($this->states[$i]["country"]==$country)
$states_list[] = $this->states[$i]["name"];
}
return $states_list;
}
}
class SubFineWorking extends WorkingFine{
protected $country = "india";
public function getStates() {
return $this->getStatesByCountry($this->country);
}
}
$objIndiaStates = new SubFineWorking();
print "<pre>";
print_r($objIndiaStates->getStates());
print "</pre>";
/**Invalid
$obj = new WorkingFine();
print_r($obj->countries);
print_r($obj->states);
print_r($obj->getState());*/
?>
|
Abstract:
- This keyword only used to a php classes and its functions.
- For Containing abstract functions, a class should be a abstract class.
- Abstract keyword not applicable in a class variable.
- We can't able access abstract class member directly(can't instantiate abstract class member)
- We can inherit the abstract class.
- Abstract class behave like php interface.
- Abstract function can't contain definition. We should only declare the abstract function.
- We should inherit the abstract class to the non abstract sub class and define the function.
class: We can able to use abstract keyword for class.
function: We can able to use abstract keyword for class functions.
function/properties: We can't use this keyword class variables.
Example:
<?php
abstract class WorkingFine{
public $countries= array("india","Unitedstate");
public $states= array(
array("name"=>"Tamilnadu","country"=>"india"),
array("name"=>"Karnataka","country"=>"india"),
array("name"=>"kerala","country"=>"india"),
array("name"=>"Andhra","country"=>"india"),
array("name"=>"Pudhucheri","country"=>"india"),
array("name"=>"Newyork","country"=>"Unitedstate"),
array("name"=>"Los Angeles","country"=>"Unitedstate"));
abstract function getStates(); //only declaration Not definition
//if you are using public function inside abstract class must contain a //body otherwise you will get the fetal error.below one public method
public function getStatesByCountry($country) {
for($i=0;$i<count($this->states);$i++) {
if($this->states[$i]["country"]==$country)
$states_list[] = $this->states[$i]["name"];
}
return $states_list;
}
}
class SubFineWorking extends WorkingFine{
protected $country = "india";
public function getStates() {
return $this->getStatesByCountry($this->country);
}
}
$objIndiaStates = new SubFineWorking();
print "<pre>";
print_r($objIndiaStates->getStates());
print "</pre>";
?>
|
Final:
class: We can able to use Final keyword for class.
function: We can able to use Final keyword for class functions.
function/properties: We can't use this keyword class variables.
It Prevents subclass to override base class member defined with final keyword.
1. Final Keyword is applicable to only class and class methods.
2. We can not declare variables as Final in PHP.
3. If you declare class method as a Final then that method can not be override by the child class. Same as method if we declare class as a Final then that class can not be extended any more.
Example:
<?php
final class parent_class
{
// We Can not override this method
// As it declared as final
final public function class_method()
{
/*
Code Here
*/
echo "test for final method";
}
}
$obj = new parent_class();
print_r($obj->class_method());
?>
|
access modifier |
classes |
functions |
variable |
public |
Not Applicable |
Applicable |
Applicable |
private |
Not Applicable |
Applicable |
Applicable |
protected |
Not Applicable |
Applicable |
Applicable |
abstract |
Applicable |
Applicable |
Not Applicable |
final |
Applicable |
Applicable |
Not Applicable |
Static:
It is mainly divided in to 2 types
1. Static variable.
2. Static keywords
If we are used Static keyword in a class methods and properties, You can directly call the class function using :: operator, No need to instantiate the class.
Classname::method();
ClassName::properties;
|
The static keyword used before or after the method's visibility.
static private function Mymethod();
private static function Mymethod();
|
$this vs self
Normally we can use $this for calling the methods and properties inside the class. If you used static keyword, we should call the method and properties using self with :: operator.
<?php
class MyClass{
static private $static_property="using self :: operator static properties";
public static function static_method(){
//print_r($this->static_property); You will get fetal error for static
echo "using self :: operator static method - ";
}
public function method(){
//this is the way of calling static properties inside the class
self::static_method();
return self::$static_property;
}
}
//we can call outside of the class using :: operator directly//
//without instance//
print_r(MyClass::method());
//Normally object using instance of the class outside of the class
$obj = new MyClass();
print_r($obj->method());
?>
|
$this
| self
|
Represents an instance of the class or object
| Represents a class
|
Always begin with dollar ($) sign
| Never begin with dollar($) sign
|
Is followed by the -> operator
| Is followed by the :: operator
|
The property name after the -> operator does not take dollar ($) sign, e.g., $this->property. | The property name after the :: operator always take the dollar ($) sign. |
Note: You must keep in mind the static variable values are not to be deleted when the function is completed/executed.
OOPS implements VS extends
extend :
- To inherit base class from child class using the extends keyword
- PHP allows only one class to extend.
public class MyClass extends BaseClass implements myInterface1, myInterface2, myInterface3{
//code
}
|
Implements:
- Similarly like extending to other classes, but only work for interface.
- The Implements keyword we used for PHP Interface.
- PHP implements keyword to allow the multiple interfaces.(above example)
Example:
<?php
abstract class Animal {
private $name;
abstract function eat($name);
abstract function sleep($name);
}
interface Pet {
public function play($name);
}
//Here extend the abstract class and implement the interface
class Dog extends Animal implements Pet {
public function eat($name) {
$this->name = $name;
return "$this->name"." can eat";
}
public function sleep($name) {
$this->name = $name;
return "$this->name"." can sleep";
}
public function play($name) {
$this->name = $name;
return "$this->name"." only can play, because of the pet animal";
}
}
public class Hippo extends Animal {
public function eat($name) {
$this->name = $name;
return "$this->name"." can eat";
}
public function sleep($name) {
$this->name = $name;
return "$this->name"." can sleep";
}
}
//$obj = new Dog();
//print_r($obj->play("DOG"));
?> |
PHP Constructor:
Constructor is a method, which is automatically executed, when Object is created.
Constructor is a special method, constructor name is similar to Class name.
"Automatically executed, when instantiate the Author class"
class Author {
public function __construct() {
return "Automatically executed, when instantiate the class"
}
}
$obj = new Author();
print_r($obj);
//output: Automatically executed, when instantiate the class |
you can pass the argument to the constructor
class Author {
private $firstName;
private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->PrintName();
}
public function PrintName(){
echo "My First name is ". $this->firstName." my second name is ". $this->lastName;
}
}
new Author('siva', 'jack');
//output: My First name is siva my second name is jack |
PHP Destructor:
Destructor is a special method, which is called automatically executed when the Object is destroyed.
Destructor is automatically called at end of the script.
The good example for where we use the constructor and destructor
<?php
class Session
{
protected $data = array();
public function __construct()
{
// load session data from database or file
}
// get and set functions
public function __destruct()
{
// store session data in database or file
}
}
|
Destructor example:
<?php
class Author {
private $firstName;
private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->PrintName();
}
public function PrintName(){
echo "My First name is ". $this->firstName." my second name is ". $this->lastName;
}
public function __destruct(){
echo "After completion the script, object destroyed automatically";
}
}
$obj = new Author('siva', 'jack');
echo is_object($obj);
//output
//
//My First name is siva my second name is jack
//1
//After completion the script, object destroyed automatically
|
Concepts:
1. Inheritance
2. Encapsulation
3. Polymorphism
4. Abstraction
5. Interface
Interface:
Interfaces like a Classes, The below features Interface modified from class.
- You can't instantiate the interface.
- You define only Public method stubs without bodies.
- You must implement all those declared methods to any class using implement keyword.
- You can extend the one interface to another using extends keyword.
<?php
interface QueryInterface {
public function whereId($id);
}
interface CustomerQueryInterface extends QueryInterface {
public function whereType($type);
}
|
Useful:
- The
implements
keyword also allows us to implement multiple interfaces. (PHP Class not possible multiple inheritance - whereas extending in PHP only allows you to extend one class.)
- Interface dependency Injection.
<?php
interface QueryInterface {
public function whereId($id);
}
interface CustomerQueryInterface {
public function whereType($type);
}
class CustomerControl implements QueryInterface, CustomerQueryInterface {
public function whereId($id){
return $id;
}
public function whereType($type){
return $type;
}
}
|
Dependency Injection:
One of the best useful is dependency injection.
Simple and best defenition for Dependency injection:
"Inject an Object to another Object."
A dependencies instantiate to the dependent class - without dependency injection
<?php
class CustomersController {
private $customerQuery;
public function __construct() {
$this->customerQuery = new CustomerQuery();
}
}
|
With dependency Injection Example:
<?php
interface QueryInterface {
public function whereId($id);
}
interface CustomerQueryInterface extends QueryInterface {
public function whereType($type);
}
class CustomerQuery implements CustomerQueryInterface {
private $id;
private $type;
public function __construct($id, $type){
$this->id = $id;
$this->type = $type;
}
public function whereId($id) {
// do something to get Customer by $id
//
return $this->id;
}
public function whereType($type) {
// do something to get Customer by $type
return $this->type;
}
}
class CustomersController {
private $customerQuery;
public function __construct(CustomerQuery $customerQuery) {
$this->customerQuery = $customerQuery;
}
}
$customerQuery = new CustomerQuery('12345', 'retail');
$obj = new CustomersController($customerQuery);
var_dump($obj);
?>
|
Dependency Injection Class Example:
<?php
class Author {
private $firstName;
private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName() {
return $this->firstName;
}
public function getLastName() {
return $this->lastName;
}
}
class Question {
private $author;
private $question;
public function __construct($question, Author $author) {
$this->author = $author;
$this->question = $question;
}
public function getAuthor() {
return $this->author;
}
public function getQuestion() {
return $this->question;
}
}
$author = new Author('jack','siva');
print_r($author);
$obj = new Question('what is your name', $author);
print_r($obj);
//print_r($obj->getAuthor());
//print_r($obj->getQuestion());
//you can inject an object to another object
?>
|
Abstraction:
The OOPS Abstraction is show only useful information remaining are hide for end user.
when a method is declared as abstract in an abstract class, Its derived classes must implement that method.
We can't able to instantiate the class directly.
Use public or Protected visibility to abstract methods.
Example:
<?php
abstract class WorkingFine{
public $countries= array("india","Unitedstate");
public $states= array(
array("name"=>"Tamilnadu","country"=>"india"),
array("name"=>"Karnataka","country"=>"india"),
array("name"=>"kerala","country"=>"india"),
array("name"=>"Andhra","country"=>"india"),
array("name"=>"Pudhucheri","country"=>"india"),
array("name"=>"Newyork","country"=>"Unitedstate"),
array("name"=>"Los Angeles","country"=>"Unitedstate"));
abstract function getStates(); //only declaration Not definition
//if you are using public function inside abstract class must contain a //body otherwise you will get the fetal error.below one public method
public function getStatesByCountry($country) {
for($i=0;$i<count($this->states);$i++) {
if($this->states[$i]["country"]==$country)
$states_list[] = $this->states[$i]["name"];
}
return $states_list;
}
}
class SubFineWorking extends WorkingFine{
protected $country = "india";
public function getStates() {
return $this->getStatesByCountry($this->country);
}
}
$objIndiaStates = new SubFineWorking();
print "<pre>";
print_r($objIndiaStates->getStates());
print "</pre>";
?>
|
Abstract Class Example;
<?php
abstract class MobilePhone
{
abstract protected function Calling(); // calling function
abstract protected function SendSMS(); // calling function
public function AccessPublic(){
echo "access to the abstract class public method from subclass";
}
}
class Nokia1400 extends MobilePhone
{
// code here for Nokia 1400 class
public function Calling(){
// body function
}
public function SendSMS(){
// body function
}
}
class Nokia2700 extends MobilePhone
{
public function FMRadio(){
// calling function
$this->AccessPublic();
echo "nokia 2700 fm radio";
}
public function MP3(){
// body function
}
public function Camera(){
// body function
}
public function Calling(){
// body function
$this->AccessPublic();
echo "Calling protected function";
}
public function SendSMS(){
// body function
}
}
class BlackBerry extends MobilePhone
{
public function FMRadio(){
// calling function
//
}
public function MP3(){
// calling function
}
public function Camera(){
// calling function
}
public function Recording(){
// calling function
}
public function ReadAndSendEmails(){
// calling function
}
public function Calling(){
// body function
}
public function SendSMS(){
// body function
}
}
$obj = new Nokia2700();
print_r($obj->Calling());
|
Inheritance:
"To share the methods and variables to child classes from parent Class"
inheritance - Using the Extends keyword to inherit the Subclass (Cow) from the (Animal class) Parent Class.
<?php
class Animal
{
private $family;
private $food;
public function __construct($family, $food)
{
$this->family = $family;
$this->food = $food;
}
public function get_family()
{
return $this->family;
}
public function set_family($family)
{
$this->family = $family;
}
public function get_food()
{
return $this->food;
}
public function set_food($food)
{
$this->food = $food;
}
}
class Cow extends Animal
{
private $owner;
public function __construct($family, $food)
{
parent::__construct($family, $food);
}
public function set_owner($owner)
{
$this->owner = $owner;
}
public function get_owner()
{
return $this->owner;
}
}
$obj = new Cow('testfamily', 'testfood');
$obj->set_owner('siva');
print_r($obj);
?>
|
Types:
1. simple/single inheritance
2. Multi-level inheritance
3. Multiple inheritance - Not supported in PHP. You can implement that using interface or PHP trait.
simple/single Inheritance:
A-->B
<?php
Class A {
//Parent class stuff
}
Class B extends A {
//simple inherit parent class from Class B child class.
}
?>
|
Multi-level inheritance:
class GrandFather is a base class , extended by Father class and this is extended by Son class
myHistory function in Son class access Father class and GrandFather class functions
GrandFather ---- Father ------Son
<?php
class GrandFather
{
public function gfAge()
{
return ' age is 80';
}
}
class Father extends GrandFather
{
public function fAge()
{
return ' age is 50';
}
}
class Son extends Father
{
public function sAge()
{
return 'age is 20';
}
public function myHistory()
{
echo "my grand father ".parent::gfAge();
echo "my father ".parent::fAge();
echo "my ".$this->sAge();
}
}
$son = new Son();
$son->myHistory();
// output is
//my grand father age is 80
//my father age is 50
//my age is 20
?>
|
Multiple inheritance
Subclass can not extend more than one super class Like below example. (PHP not support)
<?php
Class A {
//Parent class stuff
}
Class B {
// second parent class stuff
}
Class c extends A, B {
//simple inherit parent class from Class B child class.
}
?>
|
Encapsulation:
"Wrapping of some data in a single unit its called Encapsulation"
Advantages:
1. Data hiding - Class used the private method it can be accessed only within a same class. Outside of the class can't able to access the private method.
2. read and write - To make only and write only by providing setter and getter method.
Visibility is a mechanism for Encapsulation
class Person {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName($name) {
return $this->name;
}
}
$robin = new Person();
$robin->setName('Robin');
$robin->getName();
|
Polymorphism:
"Ploymorphism is used to make more modular and extensible application, in which classes have different functionality while sharing a common interface"
According to the polymorphism principle, methods in different classes that do similar things should have the same name.
Common interface, the different classes implement common interface with same method name
interface Shape {
public function name();
}
class Circle implements Shape {
public function name() {
echo "I am a circle";
}
}
class Triangle implements Shape {
public function name() {
echo "I am a triangle";
}
}
function test(Shape $shape) {
$shape->name();
}
test(new Circle()); // I am a circle
test(new Triangle()); // I am a triangle
|
OOPS Overloading, Overriding:
Overloading:
Function overloading is a feature of Object oriented programming that allows to creating a methods with the same same but differ in the type of input parameter.
Simply said, Creating a multiple methods in the same name, that functions are used perform a different task.
Overloading in PHP - Create the properties and methods dynamically using magic method.
Normally, PHP overloading methods without magic methods will get Fatal error: Cannot redeclare text::display()
<?php
class text {
public function display($parameter1) {
echo "Hello world!!";
}
public function display($parameter1,$parameter2) {
echo "Hello India!!”;
}}
$obj = new text;
$obj->display('Hello'); // It will show fatal error
?>
|
PHP overloading with same name function can’t be possible.
Overloading in PHP with magic method:__call ( string $name , array $arguments )
<?php
class TDshape {
const Pi = 3.142 ; // constant value
function __call($fname, $argument){
if($name == 'area')
switch(count($argument)){
case 0 : return 0 ;
case 1 : return self::Pi * $argument[0] ; // 3.14 * 5
case 2 : return $argument[0] * $argument[1]; // 5 * 10
}
}
}
$circle = new TDshape();
echo "Area of circle:".$circle->area(5)."</br>"; // display the area of circle
$rect = new TDshape();
echo "Area of rectangle:".$rect->area(5,10); // display area of rectangle
?>
Method Overriding:
PHP overriding is a feature of Object oriented Programming that is replace the parent methods in the Child class.
Purpose of overriding - To change the behavior of your parent class methods.
<?php
class parent_class
{
public function text() //text() is a parent class method
{
echo "Hello!! everyone I am parent class text method"."</br>";
}
public function test()
{
echo "Hello!! I am second method of parent class"."</br>";
}
}
class child extends parent_class
{
public function text() // Text() parent class method which is override by child class
{
echo "Hello!! Everyone i am child class";
}
}
$obj= new parent_class();
$obj->text(); // display the parent class method echo
$obj= new child();
$obj->text(); // display the child class method echo
$obj->test();
?>
PHP trait:
A Trait is simply a group of methods that you want include within another class. A Trait, like an abstract class, cannot be instantiated on it’s own.
<?php
trait Sharable {
public function share($item)
{
return "share this item".$item;
}
}
class Post {
use Sharable;
}
class Comment {
use Sharable;
}
$post = new Post;
echo $post->share('siva');
$comment = new Comment;
echo $comment->share('jack');
?>
|
example:
<?php
// Interface
interface Sociable {
public function like();
public function share($item);
}
// Trait
trait Sharable {
public function share($item)
{
// share this item
echo "shaable trait".$item;
}
}
// Class
class Post implements Sociable {
use Sharable;
public function like()
{
echo "must be define like method because of interface";
}
}
$obj = new Post();
$obj->like();
$obj->share('using trait define the share method in the post class');
?>
|
PHP: Magic Methods:
The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone() are magical in PHP classes. PHP reserves all function names starting with __ as magical.
We have already discussed __construct(), __destruct() and __clone() methods.
Here are other magic methods :
__call()
__call() is triggered when invoking inaccessible methods in an object context.
Syntax : public mixed __call ( string $name , array $arguments )
__callStatic()
__callStatic() is triggered when invoking inaccessible methods in a static context.
Syntax : public mixed __call ( string $name , array $arguments )
__get()
__get() is utilized for reading data from inaccessible properties.
Syntax : public mixed __get ( string $name )
__set()
__set() is run when writing data to inaccessible properties.
Syntax : public void __set ( string $name , mixed $value )
__isset()
__isset() is triggered by calling isset() or empty() on inaccessible properties.
Syntax : public bool __isset ( string $name )
__unset()
__unset() is invoked when unset() is used on inaccessible properties.
Syntax : public void __unset ( string $name )
__sleep()
__sleep() is used to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which do not need to be saved completely.
Syntax : public array __sleep ( void )
__wakeup()
__wakeup() is used to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.
Syntax : void __wakeup ( void )
__toString()
The __toString() method allows a class to decide how it will react when it is treated like a string.
Syntax : public string __toString ( void )
__invoke()
The __invoke() method is called when a script tries to call an object as a function.
Syntax : mixed __invoke ([ $... ] )
__set_state()
This static method is called for classes exported by var_export() since PHP 5.1.0.
Syntax : static object __set_state ( array $properties )
We will provide some examples of magic methods soon.
Void : It is a php return type, there no value of the function, we can use the void return type.(shouldn't return anything)