__autoload() magic method in oop

<?php

/* __autoload() magic method take important part to increase the performance of the application by dynamically include its required class …

Lets have close look at the below programme … here i create two file of class one is named a.class.php and another b.class.php .. now in raw php we call them both at a time .. it does not creat a matter for us what object of class we working in .. means if we create object of a.class.php we include both the classs .. certainly it slow up the performance little bit ..
and the __autoload() method help us to get rid of this .. it include the class only if we create an object of that class and speed up the performance … */

function __autoload($class)
{
require ($class.’.class.php’);
}
$object = new a();

$object->seta(12);
echo $object->geta();
?>

 

 

/*** **** a.class.php ********/

<?php

class a
{
private $a;
public function seta($a)
{
$this->a = $a;
}
public function geta()
{
return $this->a;
}
}

?>

 

/*** **** b.class.php ********/

 

<?php

class b
{
private $b;
public function setb($b)
{
$this->b = $b;
}
public function getb()
{
return $this->b;
}
}

?>

Advertisements

__isset() and __unset() magic method in oop

<?php
/*__isset() magic method receives a parameter – the value of which is the name of the variable that the program wants to test if the variable is set or not.

__unset() magic method receives a parameter – the value of which is the name of the variable that the program wants to unset.*/
class epico
{
private $un_var = array();
function __set($a,$b)
{
$this->un_var[$a] = $b;
}
function __get($a)
{
echo $this->un_var[$a];
}
function __isset($a)
{
return isset($this->un_var[$a]);
}
function __unset($a)
{
unset($this->un_var[$a]);
//echo “The variable is unset”;
}
}

$ob = new epico();
$ob->name = “Khandaker Fazle Elahi”;
echo isset($ob->name);
echo unset($ob->name);
?>

__sleep() and __wakeup() magic method in oop

<?php

/*__sleep() magic method automatically called when an object of class is to be serialized .. it does not accept any parameter … it return array with element name of some properties name in the class … the properties names are those that you don’t wish to be serialized …

__wakeup() magic method automatically called when an object of class is to be unserialized .. it does not accept any parameter and do not return anything …*/

class myth
{
public $a;
public $b;
function set($a,$b)
{
$this->a = $a;
$this->b = $b;
}
function get()
{
echo $this->a;
}
function __sleep()
{
return array(‘b’);
}
function __wakeup()
{
echo “After the object is unserialized !!;<br>”;
}
}

$ob = new myth();
var_dump($ob);
echo “<br>”;
$ob->set(1,2);
var_dump($ob);
echo “<br>”;
$ob2 = serialize($ob);
var_dump($ob2);
echo “<br>”;
var_dump(unserialize($ob2));
echo “<br>”;
?>

/*Check out the output when the object is serialized …*/

__set() and __get() magic method in oop

<?php
/*__set() magic method used when a value is to be assigned for an undefined variable within a class … when an undefined variable is to be assign __set() automatically called .. __set() method put those undefined variable into a array … so that it accept two parameter .. one is the index number another is the value …

__get() megic method used to show the undefined variable … display the undefined variable it called automatically ..*/

class ap
{
public $a;
public $un_var = array();

public function __set($y,$z)
{
$this->un_var[$y] = $z;
}

public function __get($y)
{
echo $this->un_var[$y];
}
}

$object = new ap();

$object->name = “Khdandaker Fazle Elahi”;
$object->age = 23;
echo $object->name.”\t “.$object->age;

?>

__clone() magic method in oop

<?php

/*__clone() method used to create a clone of a object of class .. However there is a subtle difference between cloning a object of class or assigning a object of class to another .. When we assign $b = $c; that means $b is not a new object, $b use the same memory space like the $c use this called copy … whereas cloning an object make new memory space for the cloned object it is not a copy …

Example of cloning method is given below ..

*/
class b
{
public $a;
public function seta($a)
{
$this->a = $a;
}
public function geta()
{
echo $this->a;
}
public function __clone() /*this function is so much important cause when you use clone keyword to make a clone of an object its not mandetory to have a __clone mehtod within the class .. you can run the code making the full __clone() method within comment line and you will find out a successfull output having no problem ..*/
{
$an = new b();
$an->seta($a);
$an->geta();
}
}

$bol = new b();

$bol->seta(12);
$bol->geta();

$col = clone $bol;

$col->seta(13);
echo “<br/>”;
$col->geta();
?>

 

__destruct() function example

<?php

/*A __destruct() function is a special function that is executed each time an object of a class destroy …

Its may usefull for unsetting variables or disconnect the database connection … */

class api
{
public $a;

function __construct($n)
{
$this->a = $n;
echo $this->a;

}
function __destruct()
{
echo “<br/>The Object’s property “.$this->a.” is destroyed..”;
}
}

$object = new api(‘Hello’);

?>

Serialization Example in Object Oriented PHP

<?php

/*Serialization of data means converting the data into a string of byte in such a way that you can produce original data again from the string ..

one can serialize any type of data (except type resource) …

This is actually like encoding and decoding the data … serialize() function encode the data and unserialize() function unserialize the data … this serilization may helpfull for session mechanism …*/

class freak
{
private $robot_name;

function __construct($name)
{
$this->robot_name = $name;
}
function display()
{
echo $this->robot_name;
}
}

$ins = new freak(‘x2ff4’);
$ser = serialize($ins);
$ins2 = unserialize($ser);
$ins2-> display();

?>

function overloading

<?php

/*Those who is from CSE background may know in C++ we got some knowledge about function overloading … that is two or more function of the same name in the same class at a time … they only differ by their parameter number or type ..
In object oriented php we do not have to such thing .. cause we can use facilities of function overloading using some other way .. Below example give you a sample that utilize the same thing like function overlaoding ..*/
class hacker
{
public $hacker_name;
public $hacker_age;

public function set($hn)
{
if(is_string($hn))
$this->hacker_name = $hn;
else
$this->hacker_age = $hn;
}
public function get()
{
if(isset($this->hacker_name))
echo “Name: “.$this->hacker_name;
else
echo “Age: “.$this->hacker_age;
}

}

$hacker1 = new hacker();
$hacker1->set(23);
$hacker1->get();
echo “<br/>”;
$hacker2 = new hacker();
$hacker2->set(‘Tanvir’);
$hacker2->get();
?>

Chained Subclassing example

<?php

/*Lets discuss about chained subclassing … the main theme of chained subclassing is very simple .. suppose you are a child your parents then your grandparents then your great-grandparents … this called chain .. Below i give an example of chained subclassing ..
*/
class great_grandpa
{
}

class grandpa extends great_grandpa
{
}

class father extends grandpa
{
}

class boy extends father
{
}
/*I think now you got a clear concept about the chained subclassing …*/
?>

function overriding

<?php

/*Function Override is that when child class contain same method or function whatever you say that also in the parent class … then what hapen if programmer made an object of class candy that extends the bingo class and call the showup() function that exist in both class …??

Okay ,, as the programmer create an object of candy class and candy class extends the bingo class then the showup() function will called from candy class … This is actually known as function overriding …*/
class bingo
{
function showup()
{
echo “This is Bingo’s show!!”;
}
}
class candy extends bingo
{
function showup()
{
echo “This is Candy’s show!!”;
}
}

$object = new candy();
$object -> showup();
?>