12 OOP – III in PHP
Hiren Joshi
Clone an object:
Working with objects, it may be required to copy an object. To make a copy of an object, the assignment operator (=) is used. The assignments operator just creates a new reference to the same object. In other words, both the variables points to the same object. So, if you change the properties of one object, that will also change the properties of other object too.
Let us check it by example.
<?php
class Employee
{
private $eid, $ename; // properties of a class Employee
public function __construct($eid,$ename)
{
$this->eid = $eid;
$this->ename = $ename;
}
// Getter and setter to set properties of object
public function geteid() //get method to get employeeid
{
return $this->eid;
}
public function seteid($value) //set method to set employeeid
{
$this->eid = $value;
}
//getname
public function getename()
{
return $this->ename;
}
public function setename($value)
{
$this->ename = $value;
}
}
$obj1 = new Employee(101,’Vipul’);
echo ‘<br> $obj1 is now : ‘;
print_r($obj1);
$obj2 = new Employee(110,’Dhaval’);
echo ‘<br> $obj2 is now : ‘;
print_r($obj2);
$obj1 = $obj2;
echo ‘<br> after assignment operator now object 2 is <br>’; print_r($obj2);
echo ‘<br> after assignment operator now object 1 is <br>’; print_r($obj1);
echo “<br> set name of obj2 to vimal <br>”;
$obj2->setename(‘Vimal’);
echo ‘<br> $obj2 is after setname is: <br> ‘;
print_r($obj2);
echo ‘<br> $obj1 is after obj2 setname is: <br> ‘;
print_r($obj1);
?>
Then the output will be:
$obj1 is now : Employee Object ( [eid:Employee:private] => 101 [ename:Employee:private] => Vipul )
$obj2 is now : Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
after assignment operator now object 2 is
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
after assignment operator now object 1 is
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
set name of obj2 to vimal
$obj2 is after setname is:
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Vimal )
$obj1 is after obj2 setname is:
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Vimal )
To copy the object, clone keyword is also used.
The syntax to clone an object is:
clone <objectName>;
PHP makes a shallow copy of an object when an object is copied.
Shallow copy means it copies the memory location of an object. Changes in shallow copy is reflected on original object. To not create shallow copy, cloning of an object is required.
Let us understand the use of clone by example.
<?php
class Employee
{
private $eid, $ename; // properties of a class Employee
public function __construct($eid,$ename)
{
$this->eid = $eid;
$this->ename = $ename;
}
// Getter and setter to set properties of object
public function geteid() //get method to get employeeid
{
return $this->eid;
}
public function seteid($value) //set method to set employeeid
{
$this->eid = $value;
}
//getname
public function getename()
{
return $this->ename;
}
public function setename($value)
{
$this->ename = $value;
}
}
$obj1 = new Employee(101,’Vipul’);
echo ‘<br> $obj1 is now : ‘;
print_r($obj1);
$obj2 = new Employee(110,’Dhaval’);
echo ‘<br> $obj2 is now : ‘;
print_r($obj2);
$obj1 = clone $obj2;
echo ‘<br> after cloning now object 2 is <br>’;
print_r($obj2);
echo ‘<br> after cloning now object 1 is <br>’;
print_r($obj1);
echo “<br> set name of obj2 to vimal <br>”;
$obj2->setename(‘Vimal’);
echo ‘<br> $obj2 is after setname is: <br> ‘;
print_r($obj2);
echo ‘<br> $obj1 is after obj2 setname is: <br> ‘;
print_r($obj1);
$obj2->seteid(111);
echo ‘<br> $obj2 is after seteid is: <br> ‘;
print_r($obj2);
echo ‘<br> $obj1 is after obj2 seteid is: <br> ‘;
print_r($obj1);
?>
The output is:
$obj1 is now : Employee Object ( [eid:Employee:private] => 101 [ename:Employee:private] => Vipul )
$obj2 is now : Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
after cloning now object 2 is
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
after cloning now object 1 is
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
set name of obj2 to vimal
$obj2 is after setname is:
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Vimal )
$obj1 is after obj2 setname is:
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
$obj2 is after seteid is:
Employee Object ( [eid:Employee:private] => 111 [ename:Employee:private] => Vimal )
$obj1 is after obj2 seteid is:
Employee Object ( [eid:Employee:private] => 110 [ename:Employee:private] => Dhaval )
Inspecting an object
- Inspecting an object is also known as introspection or reflection
- There are many methods available for reflection. You can find out these methods through the URL http://www.php.net/manual/en/book.reflection.php
- The following table summarize few of the very useful methods to inspecting an object.
- These methods are used to check whether an object parameter is of expected type or not.
- These methods are also used to make sure that a property or method exists before it is called.
Function | Description |
class_exists($class) | Returns true if the $class is exists |
get_class($object) | Returns the class name of $object |
is_a($object, $class) | Returns true if $object is an instance of $class |
property_exists($object, $property) | Returns true if $property exists for $object |
method_exists($object, $method) | Returns true if $method exists for $object |
Let us write a PHP code for above functions.
<?php
class Employee
{
private $eid, $ename; // properties of a class Employee
public function __construct($eid,$ename)
{
$this->eid = $eid;
$this->ename = $ename;
}
// Getter and setter to set properties of object
public function geteid() //get method to get employeeid
{
return $this->eid;
}
public function seteid($value) //set method to set employeeid
{
$this->eid = $value;
}
public function getename()
{
return $this->ename;
}
public function setename($value)
{
$this->ename = $value;
}}
$obj = new Employee(1,’Abhay’);
echo “<br> is class Employee exists: “;
//class_exists
$ans1 = class_exists(‘Employee’);
echo ” $ans1 “;
// get class
$ans2 = get_class($obj);
echo “<br> obj is object of class : $ans2”;
//is_a
$ans3 = is_a($obj,’Employee’);
if ($ans3 == true)
{
echo “<br> obj is an object of class Employee “;
}
else
{
echo “<br> obj is not an object of class Employee”;
}
//property_exists
$ans4 = property_exists($obj,’eid’);
if ($ans4 == true)
{
echo “<br> property eid exists for object obj “;
}
else
{
echo “<br> property eid does not exist for object obj “;
}
//method_exists
$ans5 = method_exists($obj,’geteid’);
if ($ans5 == true)
{
echo “<br> method geteid exists for object obj “;
}
else
{
echo “<br> method geteid does not exist for object obj “;
}
?>
The output of the above programmer is:
is class Employee exists: 1
obj is object of class : Employee
obj is an object of class Employee
property eid exists for object obj
method geteid exists for object obj
Inheritance
- Inheritance is one of the essential concepts in Object Oriented Programing.
- Inheritance allows developer to create a new class based on existing class.
- The new class inherits all the properties and methods of class which is inherited.
- The class which is inherited from the class is known as subclass or derived class or child class.
- The class which is inherited by other class is known as superclass or base class or parent class.
- The subclass can extend the inheritance by adding new properties and methods.
- The subclass can override a method of the superclass. Method overriding means subclass use the same method name defined in super class.
- A subclass can call a method of superclass. To call a super class method in subclass use the parent keyword then type :: (double colon) and then type the method name. The parent keyword refer to the super class.
- Inheritance provide reusability of code.
- Inheritance is used by the developers to develop consistent classes for large application.
The following code demonstrate an example of inheritance.
<?php
class Person
{
private $fname,$lname,$email,$mobile;
public function __construct($fname,$lname)
{
$this->fname = $fname;
$this->lname = $lname;
}
//fname
public function getfname()
{
return $this->fname;
}
public function setfname($value)
{
$this->fname = $value;
}
//lname
public function getlname()
{
return $this->lname;
}
public function setlname($value)
{
$this->lname = $value;
}
public function getemail()
{
return $this->email;
}
public function setemail($value)
{
$this->email = $value;
}
//mobile
public function getmobile()
{
return $this->mobile;
}
public function setmobile($value)
{
$this->mobile = $value;
}
}
class Employee extends Person
{
private $eid, $edept;
public function __construct($fname, $lname,$eid,$edept)
{
$this->eid = $eid;
$this->edept = $edept;
parent::__construct($fname,$lname);
}
//eid
public function geteid()
{
return $this->eid;
}
public function seteid($value)
{
$this->eid = $value;
}
//edept
public function getdept()
{
return $this->edept;
}
public function setedept($value)
{
$this->edept = $value;
}
}
$emp = new Employee(‘first’,’last’,1,’IT’);
print_r($emp);
$emp->setmobile(1234567890);
echo “<br>”;
print_r($emp);
?>
Then the output will be:
Employee Object ( [eid:Employee:private] => 1 [edept:Employee:private] => IT [fname:Person:private] => first [lname:Person:private] => last [email:Person:private] => [mobile:Person:private] => ) Employee Object ( [eid:Employee:private] => 1 [edept:Employee:private] => IT [fname:Person:private] => first [lname:Person:private] => last [email:Person:private] => [mobile:Person:private] => 1234567890 )
Access Modifier
The subclass inherits all the public and protected properties and methods while it cannot inherits private properties and methods.
Following table shows how the access modifier works
Modifier
|
Access outside the
class? |
Access from
Subclass? |
Public | Yes | Yes |
Protected | No | Yes |
Private | No | No |
Following code show the use of protected access modifier.
<?php
class Person
{
protected $fname,$lname;
private $email,$mobile;
public function __construct($fname,$lname)
{
$this->fname = $fname;
$this->lname = $lname;
}
//fname
public function getfname()
{
return $this->fname;
}
public function setfname($value)
{
$this->fname = $value;
}
//lname
public function getlname()
{
return $this->lname;
}
public function setlname($value)
{
$this->lname = $value;
}
public function getemail()
{
return $this->email;
}
public function setemail($value)
{
$this->email = $value;
}
//mobile
public function getmobile()
{
return $this->mobile;
}
public function setmobile($value)
{
$this->mobile = $value;
}
}
class Employee extends Person
{
private $eid, $edept;
public function __construct($fname, $lname,$eid,$edept)
{
$this->eid = $eid;
$this->edept = $edept;
parent::__construct($fname,$lname);
}
//eid
public function geteid()
{
return $this->eid;
}
public function seteid($value)
{
$this->eid = $value;
}
//edept
public function getdept()
{
return $this->edept;
}
public function setedept($value)
{
$this->edept = $value;
}
//fullname
public function getfullname()
{
return $this->fname.’ ‘.$this->lname;
}
}
$emp = new Employee(‘first’,’last’,1,’IT’);
print_r($emp);
$emp->setmobile(1234567890);
echo “<br>”;
print_r($emp);
$full = $emp->getfullname();
echo “<h1> $full”;
?>
The output will be:
Employee Object ( [eid:Employee:private] => 1 [edept:Employee:private] => IT [fname:protected] => first [lname:protected] => last [email:Person:private] => [mobile:Person:private] => )
Employee Object ( [eid:Employee:private] => 1 [edept:Employee:private] => IT [fname:protected] => first [lname:protected] => last [email:Person:private] => [mobile:Person:private] => 1234567890 ) first last
you can view video on OOP – III in PHP |
Reference:
1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,
2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress
3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,
4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O’Reilly Media
5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP, Wrox
6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible
7. Joel Murach, Ray Harris: Murach’s PHP and MySQL, Shroff/Murach
8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML/Javascript/DHTML/PHP , BPB Publications
9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams
10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education
11. http://www.php.net
12. http://www.w3schools.com/
13. http://www.tutorialspoint.com/
14. https://docs.oracle.com/javase/tutorial/java/concepts/
15. http://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm
16. http://www.tutorialspoint.com/php/php_object_oriented.htm
17. http://inchoo.net/dev-talk/understanding-phps-oop-basic-terms-explained/