• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
psnreddy  php-oops
 

psnreddy php-oops

on

  • 899 views

 

Statistics

Views

Total Views
899
Views on SlideShare
899
Embed Views
0

Actions

Likes
0
Downloads
30
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    psnreddy  php-oops psnreddy php-oops Document Transcript

    • Definition of a ClassA class is user defined data type that contains attributes or data members; and methods whichwork on the data members. (You will learn more about data members and methods in followingtutorials. This tutorial focuses only on learning how to create a Class in PHP5)To create a class, you need to use the keyword class followed by the name of the class. Thename of the class should be meaningful to exist within the system (See note on naming a classtowards the end of the article). The body of the class is placed between two curly brackets withinwhich you declare class data members/variables and class methods.Example of a Class:class Customer { private $first_name, $last_name; public function setData($first_name, $last_name) { $this->first_name = $first_name; $this->last_name = $last_name; } public function printData() { echo $this->first_name . " : " . $this->last_name; }}In the above program, Customer is the name of the class and $first_name/$last_name areattributes or data members. setData() and printData() are methods of a class. We will discussmore about attributes and members in the upcoming articles on PHP5 OOPS Tutorials series.Definition of an ObjectAn object is a living instance of a class. This means that an object is created from the definitionof the class and is loaded in memory. A good analogy to understand this is to compare objectswith humans - and understand that all of us (you and I) are objects. If God wants to send ahuman to earth, what is easy for Him to do? Create and define properties and attributes of eachhuman separately or create a one time template and generate objects out if it. Therefore, thisonetime template is a Class and you, I & everyone in this world is an object - that is a livinginstance of class Human.Creating Objects in PHP5 ClassTo create an object of a PHP5 class we use the keyword new. Below is the syntax style of howto create objects in PHP5:
    • $obj_name = new ClassName();In the above syntax style, $obj_name is a variable in PHP. ‘new’ is the keyword which isresponsible for creating a new instance of ClassName.class Customer { private $first_name, $last_name; public function getData($first_name, $last_name) { $this->first_name = $first_name; $this->last_name = $last_name; } public function printData() { echo $this->first_name . " : " . $this->last_name; }}$c1 = new Customer();$c2 = new Customer();In the above example $c1 and $c2 are two objects of the Customer Class. Both these objects areallocated different blocks in the memory. Look at the diagram below:Therefore, an object is a living instance of a class. Each object / living instance has its ownmemory space that can hold independent data values.Definition of an class attributeAn attribute is also know as data members and is used to hold data of a class. The data that itholds are specific to the nature of the class in which it has been defined. For example, aCustomer class would hold data related to a customer, an Order class would hold data related aan order.
    • Attributes can either be public, private or protected - the default being public.These are called Access Specifiers.Example Code:class Customer { private $first_name, $last_name; private $outstanding_amount = 0; //example of default value public function getData($first_name, $last_name) { $this->first_name = $first_name; $this->last_name = $last_name; } public function printData() { echo $this->first_name . " : " . $this->last_name; }}$c1 = new Customer();Objects as AttributesIn addition to declaring attributes as intrinsic data types (int, string, etc), you can also declaredata members as objects of another class. This is called aggregation in Object Oriented Analysisand Design (OOAD). Lets look at an example below:class Customer { private $first_name, $last_name; private $outstanding_amount = 0; //example of default value public function getData($first_name, $last_name) { $this->first_name = $first_name; $this->last_name = $last_name; } public function printData() { echo $this->first_name . " : " . $this->last_name; }}class Order { private $order_id; private $customer; public setCust(Customer $c) { $this->customer = $c; }}
    • $c1 = new Customer();$o1 = new Order();$o1->setCust($c1); //storing $c1 object in $o1 order objectIn the above example setCust() method accepts a Customer type of parameter which is storedinternally in the $customer data member.The advantage of the above method is that it allows you to change the customer objectindependently of the order object. Imagine having to add 3 - 4 new data members to theCustomer object. You only have to modify the Customer object without having the need tomodify the Order Object.Definition of Access SpecifiersAccess specifiers specify the level of access that the outside world (i.e. other class objects,external functions and global level code) have on the class methods and class data members.Access specifiers can either be public, private or protected.Why do we need Access specifiersAccess specifiers are used as a key component of Encapsulation and Data Hiding. By usingeither of the access specifiers mentioned above i.e. public, private or protected you can hide orshow the internals of your class to the outside world.Explanation of each access specifier1. Private2. Protected3. Public1. PrivateA private access specifier is used to hide the data member or member function to the outsideworld. This means that only the class that defines such data member and member functions haveaccess them. Look at the example below:Example:class Customer { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; }}
    • $c = new Customer();$c->setName("Sunil Bhatia");echo $c->name; //error, $name cannot be accessed from outside the class //$name can only be accessed from within the classecho $c->getName(); //this works, as the methods of the class have access //to the private data members or methodsIn the above example, echo $c->name will give you an error as $name in classCustomer has been declared private and hence only be accessed by its memberfunctions internally. Therefore, the following line echo $c->getName() willdisplay the name.2. PublicA public access specifier provides the least protection to the internal datamembers and member functions. A public access specifier allows the outsideworld to access/modify the data members directly unlike the private accessspecifier. Look at the example below:Example:class Customer { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; }}$c = new Customer();$c->setName("Sunil Bhatia");echo $c->name; // this will work as it is public.$c->name = "New Name" ; // this does not give an error.In the above example, echo $c->name will work as it has been declared as publicand hence can be accessed by class member functions and the rest of the script.3. ProtectedA protected access specifier is mainly used with inheritance. A data member or member functiondeclared as protected will be accessed by its class and its base class but not from the outsideworld (i.e. rest of the script). We can also say that a protected data member is public for the class
    • that declares it and it’s child class; but is private for the rest of the program (outside world).Look at the example below:class Customer { protected $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; }}class DiscountCustomer extends Customer { private $discount; public function setData($name, $discount) { $this->name = $name; //this is storing $name to the Customer //class $name variable. This works // as it is a protected variable $this->discount = $discount; }}$dc = new DiscountCustomer();$dc->setData("Sunil Bhatia",10);echo $dc->name; // this does not work as $name is protected and hence // only available in Customer and DiscountCustomer classIn the above example, echo $dc->name will not work work $name has been defined as aprotected variable and hence it is only available in Customer and DiscountCustomer class.Important Note of Access Specifier in PHP5In PHP5, access specifiers are public by default. This means that if you don’t specifyan access specifier for a data member or method then the default ‘public’ isapplicableDefinition of an class methodA class method/functions is the behavior/functionality of a class i.e. they provide the necessarycode for the class in which it is defined. Examples could be a saveCustomer() method in the classCustomer or a printDocument() in the Document class.
    • Methods act (perform operations) on the data members of the class and can be declared asprivate or public. A class method is exactly similar to PHP functions, it’s just that class functionsare declared inside classes and accessed using the -> (arrow operator / dereferencing operator).Methods can also be declared as either public, protected or private.Example Code:class Customer { private $name; public functionsetName($name) { $this->name = $name; }}$c1 = new Customer();$c1->setName("Sunil Bhatia");In the above example setName() is the class method of the Customer class. The setName() classmethod is responsible for accepting the name of the customer and storing it in the internal datamember i.e. $name.class Customer { private $name; public function setName($name) { if(trim($name) != "") { $this->name = $name; return true; } else { return false; } }}$c1 = new Customer();$c1->setName("Sunil Bhatia");In the above example the setName() method accepts a customer’s name and validates to check if$name is blank. If $name is blank the setName() function returns false; otherwise it stores the$name in the $this->name of the class and returns true.
    • Definition of a ConstructorA constructor is a special function of a class that is automatically executed whenever an objectof a class gets instantiated.It’s special because it is automatically executed or called when an object of a classis created.Why do we need a Constructor?It is needed as it provides an opportunity for doing necessary setup operations likeinitializing class variables, opening database connections or socket connections,etc. In simple terms, it is needed to setup the object before it can be used.In PHP5 a constructor is defined by implementing the __construct() method. Thisnaming style has been introduced in PHP5. In PHP4, the name of the constructorwas the same name as that of the class.PHP5 to be backward complaint also supports the PHP4 rule. When an object iscreated, PHP5 searches for __construct() first. If __construct() is not defined it thensearches for a method with the same that of the class. However, if you define both;PHP5 will first search for __construct() method and execute it if available, otherwiseit will execute the same class name function.class Customer { public function __construct() { //code }}Exampleclass Customer { private $first_name; private $last_name; private $outstanding_amount; public function __construct() { $first_name = ""; $last_name = ""; $outstanding_amount = 0; }
    • public function setData($first_name, $last_name, $outstanding_amount){ $this->first_name = $first_name; $this->last_name = $last_name; $this->outstanding_amount = $outstanding_amount; } public function printData() { echo "Name : " . $first_name . " " . $this->last_name . "n"; echo "Outstanding Amount : " . $this->outstanding_amount ."n"; }}$c1 = new Customer();$c1->setData("Sunil","Bhatia",0);In the above example on line number 26, we create a new object of theCustomer class. the ‘new’ operator is responsible for creating the Customerclass. At this point PHP5 searches the Customer class to see if a constructorhas been defined. Therefore, it calls the constructor method i.e.__construct() defined starting from line no 7. The __construct() method setsthe $first_name and $last_name to blank and sets the $outstanding_amount tozero.Parameterized Constructor or Argument ConstructorA parameterized or argument constructor is a constructor which accepts values in the form ofarguments in the constructor. Unlike other programming languages where overloaded argumentconstructors is possible, in PHP5 you cannot overload constructors.Example:class Customer { private $first_name; private $last_name; private $outstanding_amount; public function __construct($first_name, $last_name,$outstanding_amount) { $this->setData($first_name, $last_name, $outstanding_amount); } public function setData($first_name, $last_name, $outstanding_amount){ $this->first_name = $first_name; $this->last_name = $last_name; $this->outstanding_amount = $outstanding_amount; } public function printData() {
    • echo "Name : " . $first_name . " " . $this->last_name . "n"; echo "Outstanding Amount : " . $this->outstanding_amount ."n"; }}$c1 = new Customer("Sunil","Bhatia",0);In the above example on line number 24, we create a new object $c1 and passvalues “Sunil”, “Bhatia” and zero to the constructor defined on line numberstarting 7. The constructor now takes 3 arguments and stores them in theinternal private variable $first_name, $last_name and $outstanding_amountrespectively.Definition of a DestructorA destructor is a special function of a class that is automatically executedwhenever an object of a class is destroyed.An object of a class is destroyed Means 1. it goes out of scope, 2. when you specifically set it to null, 3. when you unset it or when the program execution is over.A PHP5 destructor is defined by implementing the __destruct() method. In PHP4 however, theconcept of a destructor did not exist.Important Note:A destructor cannot take any arguments.class Customer { public function __destructor() { //code }}Example:class Customer { private $first_name; private $last_name; private $outstanding_amount; public function __construct() { $first_name = ""; $last_name = "";
    • $outstanding_amount = 0; } public function setData($first_name, $last_name, $outstanding_amount){ $this->first_name = $first_name; $this->last_name = $last_name; $this->outstanding_amount = $outstanding_amount; } public function printData() { echo "Name : " . $first_name . " " . $last_name . "n"; echo "Outstanding Amount : " . $outstanding_amount . "n"; }}class Order { private $order_id; private $customer; public function __construct($order_id, $customer) { $this->order_id = $order_id; $this->customer = $customer; } public function __destruct() { unset($this->order_id); unset($this->customer); }}$order_id = "L0001";$c1 = new Customer();$c1->setData("Sunil","Bhatia",0);$o = new Order($order_id, $c1);In the above example on line number 45, we create a new object of the Orderclass. The argument constructor of the Order class takes two parameters i.e.$order_id and $customer object. After the program completes its execution, theobject goes out of scope because the program stops execution after line 45 andhence the destructor is automatically called.Accessor Methods:Accessor methods are also know as getter methods. The reason why we need an accessor methodis to be able to read the value of a property/attribute in a class object. In real OOAD practicemost of the data members that you define would either be private or protected (more on this willbe covered in the tutorial on Access specifiers), therefore to access data of such data members
    • that have been defined as either private or protected will require an implementation of accessoror getter methods.Note: To make a property or data member as non-read only; you should not provide a getter oraccessor method.Mutator Methods:Mutator methods are opposite to accessor methods. Mutator methods provides a mechanism tostore data in data members that have either been declared as private or protected. The reason whyyou should provide a mutator method is to provide necessary validation on the data that is to bestored in the data member of the class.Note: To make a property or data member as read only; you should not provide a setter ormutator method.Lets look at an example of accessor and mutator methods below:class Customer { private $name; //mutator method public function setName($name) { if(trim($name) != "") { $this->name = $name; return true; } else { return false; } } //accessor method public getName() { return $this->name; }}$c1 = new Customer();$c1->setName("Sunil Bhatia");echo $c1->getName();Output:Sunil BhatiaIn the above example the setName() method accepts a customer’s name and validates to check if$name is blank. If $name is blank the setName() function returns false; otherwise it stores the$name in the $this->name of the class and returns true. The getName() returns the name stored inthe $name data member of the $c1 object.
    • PHP5 introduces a new operator by the name of instanceOf. instanceOf is used to check if twoobjects passed as operands belong to the same class. This check can also happen when an objectis compared with a class name.In PHP4 a similar functionality existed with a method is_a(), which has been replaced by theinstanceOf operator in PHP5.class Person { ...}$p1 = new Person();$p2 = new Person();if($p1 instanceof $p2) echo "True";else echo "False";In the above example, we are comparing to check if $p1 and $p2 belong to the same class i.e.Person. In this case $p1 and $p2 both belong to the same class Person and hence the output isTrue. Please note that line number 8 can also be written as if($p2 instanceof $p1) and will yieldthe same output.You can also use instanceOf operator to compare an object with the name of the class, look at theexample below:class Person { ...}$p1 = new Person();if($p1 instanceof Person) echo "True";else echo "False";In the above example, on line number 7 $p1 object is being compared to check if its a Persontype of object. In this case $p1 is a Person type of object and hence the output is True.Behaviour of instanceOf operator in inheritanceclass Customer extends Person { ...}$p1 = new Person();$c1 = new Customer();
    • if($c1 instanceof $p1) echo "True";else echo "False";In the above example, on line number 8 $c1 child class object is being compared with $p1 whichis a parent class object. This is possible because Customer class is a child of the Person Class,just that the Customer class is a specialized form of the Person class and therefore this becomespossible. However the reverse is not possible, we cannot compare if($p1 instanceof $c1) and willresult in an error.ConstantsTo declare a constant in a class, PHP5 provides developers with a new keyword i.e. const; lookat the example below:class Customer { const TYPES = "Anything";}echo "Types are : " . Customer::TYPES;In the above example, const is a keyword and TYPES is the name of the constant. Outside theclass definition we echo the value of the constant by using the scope resolution operator (::) likethis Customer::TYPES. Observe that we don’t need to create an object of the class to make useof the constant.The next logical question is if we can create an object of the Customer class and using the scoperesolution operator access the constant. The answer is no; reason - because a constant belongs tothe class definition scope and not to an object.Example of accessing Constants within a functionclass Customer { const TYPES = "Anything"; public function showConstant() { echo "Echo from showConstant() : " . Customer::TYPES; }}$c = new Customer();$c->showConstant();Output:Echo from showConstant() : Anything
    • Some observations on Constants 1. Variables defined as constants cannot be changed. 2. Only a string or numeric value can be assigned to a constant. 3. Arrays, Objects & Expressions cannot be assigned to a constant. 4. A class constant can only be accessed via the scope resolution operator (::) executed on the class name. 5. A class constant cannot have <a>access specifiers</a> assigned to it (private, public & protected)Definition of Inheritance:Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class.These inherited attributes and behaviors are usually modified by means of extension.PHP5 InheritanceTo inherit in PHP5, you should use the keyword ‘extends’ in the class definition. In PHP5 onlysingle inheritance is allowed. Look at the example below:class Person { private $name; private $address; public function getName() { return $this->name; }}class Customer extends Person { private $customer_id; private $record_date; public getCustomerId() { return $this->customer_id; } public getCustomerName() { return $this->getName();// getName() is in Person }}In the above example, class Customer extends from the Person class. This means that Customerclass is the child class and the Person base class. The child class Customer extends the methodgetName() and calls it in the getCustomerName() method of the Customer class.
    • Inheritance and Access SpecifiersYou can refer to the Access specifiers tutorial to understand what it means. Let’s understand theuse of Access Specifiers with context to Inheritance.Access specifiers specify the level of access that the outside world (other objects and functions)have on the data members / member functions of the class. Please note that all data members andmember functions are public by default.Lets look at how three access specifiers viz. private, public and protected behave in Inheritance:1. privateA private access specifier is used to hide data and member functions. A method or data memberdeclared as private can only be accessed by the class itself and neither the outside program northe derived class can have access to it. Lets look at an example below:class Customer { private $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; }}$c = new Customer("Sunil","28");echo "Name : " . $c->name; //causes an errorIn the above example, the statement;echo “Name : ” . $c->name;causes an error as we are trying to access $name that has been declared as a private membervariable. We can however access the $age data member without any limitation as its public.Note: When you define a method as private that method can only be called from within that classand not from outside that is the global level script.2. publicA public access specifier allows the data members and methods to be access from anywhere inthe script. Lets look at an example below:class Customer { private $name; public $age; public function __construct($name, $age) {
    • $this->name = $name; $this->age = $age; }}$c = new Customer("Sunil","28");echo "Age : " . $c->age; //prints 28In the above example, the statement;echo “Age : ” . $c->;age;prints 28 on the screen as $age is a public variable and hence can be accessed from anywhere inthe script. Please note that if you declare any data member or method without a access specifier itis considered as ‘public’.3. protectedA protected access specifier allows the derived class to access the data member or memberfunctions of the base class, whereas disallows global access to other objects and functions.class Person { protected $name;}class Customer extends Person { function setName($name) { //this works as $name is protected in Person $this->name = $name; }}$c1 = new Customer();$c1->setName("Sunil");$c1->name = "Sunil"; //this causes error as $name is protected and not publicIn the above example, the statement;$this->name = $name;in the setName() function is referring to the $name data member of the Person class. This accessis only possible because the $name variable has been declared as protected. Had this beenprivate; the above statement would have raised an error. Further, in the statement towards theend;$c1->name = “Sunil”;raises an error as $name in the Person class has been declared as protected and not public.
    • PHP5 Inheritance - Method OverridingLets learn how to override methods in PHP5, but before we understand how to override methodsin PHP5; lets define method overriding.Definition of Method Overriding:Method overriding is when the function of base class is re-defined with the same name, functionsignature and access specifier (either public or protected) of the derived class.The reason to override method is to provide additional functionality over and above what hasbeen defined in the base class. Imagine that you have a class by the name of Bird from whichyou derive two child classes viz. Eagle and Swift. The Bird class has methods defined to eat, fly,etc, but each of the specialized classes viz Eagle and Swift will have its own style of flying andhence would need to override the flying functionality.Lets look at an example with Bird:class Bird { public function fly() { echo "Fly method of Bird Class called"; }}class Eagle extends Bird { public function fly() { echo "Fly method of the Eagle Class called"; }}class Swift extends Bird { public function fly() { echo "Fly method of the Swift Class called"; }}$e = new Eagle();$s = new Swift();$e->fly();echo "n";$s->fly();Output:Fly method of the Eagle Class calledFly method of the Swift Class calledIn the above example, we create two objects of class Eagle and Swift. Each of these classes haveoverridden the method fly() and have provided their own implementation of the fly() method thathas been extended from the Bird class. The manner in which they have been extended the Birdclass fly() method is not called as both these classes have provided a new functionality for thefly() method.
    • Another example of function overriding in Inheritanceclass Person { function calculateAge($dob) { echo "calculateAge called of Person Classn"; }}class Customer extends Person { function calculateAge($dob) { echo "calculateAge called of Customer Classn"; }}$c1 = new Customer();$p1 = new Person();$c1->calculateAge("something");$p1->calculateAge("something More");Output:calculateAge called of Customer ClasscalculateAge called of Person ClassPHP5 Inheritance - Invoking parent methodsWhen you override a method of the base class, it’s functionality is completely hidden unless ithas been explicitly invoked from the child class. To invoke a parent class method you should usethe keyword parent followed by the scope resolution operator followed by the name of themethod as mentioned below: parent::function_name();Look at the example below:class Person { public function showData() { echo "This is Persons showData()n"; }}class Customer extends Person{ public function showData() { parent::showData(); echo "This is Customers showData()n"; }}$c = new Customer();$c->showData();
    • Output:This is Person’s showData()This is Customer’s showData()In the above example, look at the way in which the showData() function in the Customer childclass is invoking the the Person parent class’s showData() function. When the program executesthe showData() method if the Customer class is called which inturn calls the showData() functionof the parent class. After the parent class’s showData() function complets its execution theremaining code in showData() function of the Customer class is executed.PHP5 Inheritance - Invoking parent Constructor and DestructorWe can get the parent PHP5 constructor and PHP5 Destructor to be invoked in the same way asinvoking the parent method, refer to the example below:class Person{ public function __construct() { echo "This is Persons __construct()n"; } public function __destruct() { echo "This is Persons __destruct()n"; }}class Customer extends Person{ public function __construct() { parent::__construct(); echo "This is Customers __construct()n"; } public function __destruct() { parent::__destruct(); echo "This is Customers __destruct()n"; }}$c = new Customer();Output:This is Person’s __construct()This is Customer’s __construct()This is Person’s __destruct()This is Customer’s __destruct()What is an exception?
    • An exception is a logical/system error that occurs during the normal execution of a script. Theexception could either be raised by the system or the program itself it the exception cannot behandled and the caller script/function needs to be informed about the same.The use of a try…catch blockPHP5 introduces the try…catch block to trap exceptions. Look at the example below.try { check();}catch(Exception $e) { echo "Message : " . $e->getMessage(); echo "Code : " . $e->getCode();}function check() { if(some error condition) { throw new Exception("Error String",Error Code); }}In the above example, the method check() is called between the try {} block. The try{} block isthe area where you will place your code that could raise an exception. Below the try{} block isthe catch() {} block. The catch block expects the Exception type of object as a parameter. Withinthe catch() {} block you will place your logic to either fix the issue or log the error.In the function check(), we raise an Exception using the ‘throw’ keyword. The statementfollowing ‘throw’ is the syntax of creating a new object of Exception type. The exception classaccepts two parameters. The left parameter is a string that is the error message and the rightparameter is the integer error code that you wish to assign to that error.Anatomy of PHP5 Exception classclass Exception { protected $message; protected $code; protected $file; protected $line; private $string; private $trace; public function __construct($message = null, $code = 0); public function __toString(); final public function getCode();
    • final public function getMessage(); final public function getFile(); final public function getLine(); final public function getTrace(); final public function getTraceAsString(); final private __clone();}In the above example, except for __construct and __toString(), no other method can beoverridden as all other methods are ‘final’.Extending the Exception classYou can also extend the exception class as follows:class CustomerException extends Exception { public function __construct($message = null, $code = 0) { $t_message = "Exception raised in CustomerException "; $t_message .= "with message : " . $message; parent::__construct($t_message, $code); }}function testException() { throw new CustomerException("CustomerException has been raised",101);}try { testException();}catch(CustomerException $e) { echo "Error Message : " $e->getMessage(); echo "Error Code : " $e->getCode();}Output:Error Message : CustomerException has been raisedError Code : 101What is an Abstract Class?
    • An abstract class is a class with or without data members that provides some functionality andleaves the remaining functionality for its child class to implement. The child class must providethe functionality not provided by the abstract class or else the child class also becomes abstract.Objects of an abstract and interface class cannot be created i.e. only objects of concrete class canbe createdTo define a class as Abstract, the keyword abstract is to be used e.g. abstract class ClassName{}Example of Abstract Classabstract class Furniture { private $height, width, length; public function setData($h, $w, $l) { $this->height = $h; $this->width = $w; $this->length = $l; } //this function is declared as abstract and hence the function //body will have to be provided in the child class public abstract function getPrice();}class BookShelf extends Furniture { private $price; public setData($h, $w, $l, $p) { parent::setData($h, $w, $l); $this->price = $p; } //this is the function body of the parent abstract method public function getPrice() { return $this->price; }}In the above example, the method getPrice() in class Furniture has been declared as Abstract.This means that its the responsibility of the child class to provide the functionality of getPrice().The BookShelf class is a child of the Furniture class and hence provides the function body forgetPrice(). Private methods cannot be abstract
    • If a method is defined as abstract then it cannot be declared as private (it can only be public orprotected). This is because a private method cannot be inherited.abstract class BaseClass { private abstract function myFun();}class DerivedClass extends BaseClass { public function myFun() { //logic here }}$d = new DerivedClass(); //will cause errorWhat is an Interface?An interface is a contract between unrelated objects to perform a common function. An interfaceenables you to specify that an object is capable of performing a certain function, but it does notnecessarily tell you how the object does so, this means that it leaves for classes implementing aninterface to define its behaviour.To extend from an Interface, keyword implements is used.We can have a class extend from more than one Interface.interface Storable { function getContentsAsText();}class Document implements Storable { public function getContentsAsText() { return "This is Text of the Documentn"; }}class Indexer { public function readAndIndex(Storable $s) { $textData = $s->getContentsAsText(); //do necessary logic to index echo $textData; }}$p = new Document();$i = new Indexer();$i->readAndIndex($p);
    • In the above example, Document and the Indexer class are two independant classes. The Indexerclass is designed to index the contents of any text. Using the Storable interface above, we declarea method getContentsAsText() in the Document class. Because the Indexer class is onlyconcerned with the TEXT, hence we can call getContentsAsText() method on the object ofDocument. This way any class if it implements the method getContentsAsText() can get indexedDifference between Abstract Class and InterfaceAbstract Classes 1. An abstract class can provide some functionality and leave the rest for derived class 2. The derived class may or may not override the concrete functions defined in base class 3. The child class extended from an abstract class should logically be relatedInterface 1. An interface cannot contain any functionality. It only contains definitions of the methods 2. The derived class must provide code for all the methods defined in the interface 3. Completely different and non-related classes can be logically be grouped together using an interfaceMeaning of PolymorphismPolymorphism is derived from two Greek words. Poly (meaning many) and morph (meaningforms). Polymorphism means many forms. In C you have two methods with the same name thathave different function signatures and hence by passing the correct function signature you caninvoke the correct method.This is how polymorphism is achieved in languages like C where in a function sum(int, int)differs from sum(float, float). Therefore the method sum() has many forms depending on theparameters being passed to it.The meaning with Object Oriented languages changes. With Object Oriented languagepolymorphism happens:When the decision to invoke a function call is made by inspecting the object at runtime it iscalled PolymorphismWhy method polymorphism cannot be achievedThe reason why polymorphism for methods is not possible in PHP is because you can have amethod that accepts two parameters and call it by passing three parameters. This is because PHPis not strict and contains methods like func_num_args() and func_get_arg() to find the number ofarguments passed and get a particular parameter.
    • Because PHP is not type strict and allows variable arguments, this is why method polymorphismis not possible.PHP 5 PolymorphismSince PHP 5 introduces the concept of Type Hinting, polymorphism is possible with classmethods. The basis of polymorphism is Inheritance and overridden methods.Lets look at an example:class BaseClass { public function myMethod() { echo "BaseClass method called"; }}class DerivedClass extends BaseClass { public function myMethod() { echo "DerivedClass method called"; }}function processClass(BaseClass $c) { $c->myMethod();}$c = new DerivedClass();processClass($c);In the above example, object $c of class DerievedClass is executed and passed to theprocessClass() method. The parameter accepted in processClass() is that of BassClass. Withinthe processClass() the method myMethod() is being called. Since the method is being called onthe class variable of BaseClass, it would not be wrong to assume that myMethod() of classBaseClass will be called. But, as per the definition “When the decision to invoke a function callis made by inspecting the object at runtime it is called Polymorphism”, myMethod() will becalled on object DerievedClass. The reason why this happens is because the object ofDerievedClass is being passed and hence the method myMethod() of DerievedClass will becalled.Why method polymorphism cannot be achievedThe reason why polymorphism for methods is not possible in PHP is because you can have amethod that accepts two parameters and call it by passing three parameters. This is because PHPis not strict and contains methods like func_num_args() and func_get_arg() to find the number ofarguments passed and get a particular parameter.
    • Because PHP is not type strict and allows variable arguments, this is why method polymorphismis not possible.PHP 5 PolymorphismSince PHP 5 introduces the concept of Type Hinting, polymorphism is possible with classmethods. The basis of polymorphism is Inheritance and overridden methods.Lets look at an example:class BaseClass { public function myMethod() { echo "BaseClass method called"; }}class DerivedClass extends BaseClass { public function myMethod() { echo "DerivedClass method called"; }}function processClass(BaseClass $c) { $c->myMethod();}$c = new DerivedClass();processClass($c);In the above example, object $c of class DerievedClass is executed and passed to theprocessClass() method. The parameter accepted in processClass() is that of BassClass. Withinthe processClass() the method myMethod() is being called. Since the method is being called onthe class variable of BaseClass, it would not be wrong to assume that myMethod() of classBaseClass will be called. But, as per the definition “When the decision to invoke a function callis made by inspecting the object at runtime it is called Polymorphism”, myMethod() will becalled on object DerievedClass. The reason why this happens is because the object ofDerievedClass is being passed and hence the method myMethod() of DerievedClass will becalled.PHP magic methods
    • PHP5 provides a magic method by the name of __toString() (double underscore followed bytoString()) which is useful for debugging purposes.The __toString() method is automatically called when an object in PHP5 is converted into astring for the purpose of display or concatenationFollowing is the example of the __toString() method:<?phpclass Customer {private $firstName, $lastName, $email;public function __construct($firstName, $lastName, $email) {$this->firstName = $firstName;$this->lastName = $lastName;$this->email = $email;}public function __toString() {return “Debug message from Customer Class : First Name = ” . $this->firstName . “, Last Name= ” . $this->lastName . “, Email = ” . $this->email;}}$c = new Customer(”Sunil”,”Bhatia”,”email@domain.com”);echo “Customer Object is >>” . $c;?>Output:Customer Object is >> Debug message from Customer Class : First Name = Sunil, Last Name =Bhatia, Email = email@domain.comSee how in this example $c Customer Object got converted into a string type when used with thedot (.) concatenation operator. In the background the magic method __toString() is automaticallycalled when such a conversion happens.Security Tip:Be careful not to include sensitive data as part of the output as you could compromise security byleaking secure information. Many applications are written to write object states in a log file,therefore you should ensure that sensitive information like Credit Card information, etc is notmade available through the magic method __toString()
    • By default PHP is a Loosely typed language and therefore it is not necessary to declare variablesbefore using them. This also holds true for using class members. Look at an example below.<?phpclass Customer {public $name;}$c = new Customer();$c->name = “Sunil”; // $name is set because its public$c->email = “email@domain.com”; //assigning email@domain.com to the $email variable.?>Ideally in a strict language this would have been an error. But, with PHP this works perfectlywell as you can assign values to an undefined variable.Because of the above limitation, PHP engine provides two magic methods __get() and __set().__get() is used when value from an undefined variable is to be read and __set() is used when avalue is to be assigned to a undefined variable of a class.__set() allows you to provide functionality to validate data being stored. See example below:<?phpclass Customer {public $name;private $data = array();public function __set($dt, $vl) {$this->data[$dt] = $vl;}public function __get($dt) {return $this->data[$dt];}}$c = new Customer();$c->name = “Sunil”; // $name is set because its public$c->email = “email@domain.com”; //assigning email@domain.com to the $email variable.
    • echo $c->email;?>In the above example when email@domain.com is assigned to the undefined variable $email, themagic method __set() is called. To this __set() method the name of the variable is passed into$dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable ofthe __set() method.The next step is to store these values into the $data array so that you could retrieve it later.The __get() method works in the similar fashion. When you echo $c->email, __get() method iscalled and the name email is passed in the $dt of the __get() method.Tip:It is possible to stop this behavior of PHP to assign values to undefined issues. The solution isthat you raise an exception from within __set() method. Look at the code below:<?class Customer {private $name;public function __set($dt, $vl) {throw new Exception(”Cannot assign values to undefined variables”,1);}}$c = new Customer();$c->email = “email@domain.com”; //this will cause an exception to be raised?>magic methods __isset() and __unset()These methods are automatically called internally when isset() and unset() is called onundeclared data members. The magic method __isset() method receives an argument - the valueof which is the name of the variable that the program wants to test if the variable is set or not.The magic method __unset() method receives an argument - the value of which is the name ofthe variable that the program wants to unset.
    • Look at the example below:class Customer { private $data = array(); public function __set($dt, $vl) { $this->data[$dt] = $vl; } public function __get($dt) { return $this->data[$dt]; } public function __isset($dt) { return isset($this->data[$dt]); } public function __unset($dt) { return unset($this->data[dt]); }}$c = new Customer();$c->name = “Sunil Bhatia”;echo isset($c->name).”n”;echo unset($c->name);In the example above the script creates a new Customer Object. The program assigns a stringvalue to an undeclared variable i.e. $c->name. The undeclared variable is handled by the magicmethod __set().The program ties to check if the undeclared variable i.e., $c->name has been set or not using thePHP method isset(). Since $c->name is an undeclared variable the PHP5 magic method __isset()is invoked that takes the name of the undeclared variable i.e. ‘name’ and checks if the internalarray $data[’name’] is set or not.Similarly, when the program calls unset() on the undeclared variable i.e. $c->name, the PHP5magic method __unset() is invoked that takes the name of the undeclared variable i.e. ‘name’ andunsets the internal array $data[’name’].The magic method __call() is to undeclared methods what __get() and __set() are to undeclareddata member.These methods are automatically called internally when the program tires to execute a methodthat has not been defined within the class at the time of development.
    • The magic method __call() takes two arguments. The first argument is the name of theundeclared method invoked by the program and the second is an array that contains a list ofparameters passed to the undeclared array.Look at the example below:class Customer { public function __call($name, $args) { var_dump($name); echo "n"; var_dump($args); echo "n"; }}$c = new Customer();$c->setName("Sunil","Bhatia");Output:string(7) “setName”array(2) {[0]=>string(5) “Sunil”[1]=>string(6) “Bhatia”}In the example above, an object of the Customer class is created and an undeclared method viz.$c->setName is called. The magic method __call() is internally executed which accepts twoparameters. The first parameter ‘$name’ contains the name of the method i.e. ’setName’ and thesecond parameter ‘$args’ contains the arguments passed to the ’setName’ method i.e ‘Sunil’ &‘Bhatia’.Using this method, you can provide code to handle calls to undeclared method. To disallowprograms to call an undeclared method, you should raise an exception from within __call()magic method.Look at the example below:class Customer { public function __call($name, $args) { throw new Exception("Undeclared method execution notallowed",10); }}
    • $c = new Customer();$c->setName("Sunil","Bhatia");Output:Fatal error: Uncaught exception ‘Exception’ with message ‘Undeclared method execution notallowed’ in D:sunilbwebsiteprogsmagic_call.php:6Stack trace:#0 [internal function]: Customer->__call(’setName’, Array)#1 D:sunilbwebsiteprogsmagic_call.php(11): Customer->setName(’Sunil’, ‘Bhatia’)#2 {main}thrown in D:sunilbwebsiteprogsmagic_call.php on line 6In the above program, when the script calls an undeclared variable $c->setName(), the magicmethod __call() is executed. On executing the magic method __call(), an exception is raisedand the execution of the program stops there (unless we use the try..catch statements)Many people debate that the magic method __autoload() causes a performance overhead. Well,that is not the case. There is no performance penalty to pay. In fact, there may be performanceimprovements if not all classes are used all the time. This is explained below.Using the magic method __autoload has the beneficial side effect of requiring strict namingconventions for files that hold class definitions.Look at the example below:include “customer.php”;include “orders.php”;$c = new Customer();In the example displayed above, an instance of class Customer is created. Therefore, we onlyneed the customers.php file. The file orders.php is not needed. This means that we should onlyhave included the customer.php file. But, what if during execution on the basis of a condition, aninstance of class Orders would have to be created. Therefore you need to include both the filesi.e. customer.php and orders.phpBut this causes performance issues. Each time the above script is executed, orders.php isincluded. To avoid this performance hit, we would have to do additional programming to ensurethat the file orders.php is loaded only when needed.This is the reason why magic method __autoload() should be used. Look at the example below:
    • function __autoload($class) { require $class . .php; //is substituted as require Customer.php (withcapital C)}$c = new Customer();In the above program, we don’t explicitly include customer.php and orders.php file.When an instance of the customer class is to be created, the PHP engine checks tosee if the file Customer.php is loaded. It does not raise an warning on finding thatCustomer.php has not been loaded, it in turn calls the magic method __autoload().The __autoload() magic method accepts a parameter which is the name of the classthat needs to be loadedTherefore, on the line when an instance of the customer class is created i.e. object $c, magicmethod __autoload() is called with the parameter $class containing value ‘Customer’. Withinthe __autoload() method we call the ‘require’ method. The require method tries to load$class.’php’ file i.e. Customer.php. Therefore, as stated earlier, the __autoload() method has itsbeneficial side effect of requiring strict file naming convention.The __autoload() method is called only once for each new class that needs to be loaded.Subsequent instantiation of the Customer class object will not call the __autoload() methodagain. Therefore, this offers performance improvements in your scripts because, unless the classis needed - files are not loaded. Therefore, the PHP engine does not have to parse and compile anunnecessary file.Working with the magic method __sleep()__sleep() magic method is called when the object of a class is about to be serialized. This magicmethod __sleep() does not accept any parameter and returns an array. The array should contain alist of class members that should be serialized. This means that if you don’t wish to serialize aparticular class member, you should not include it in the array. Look at the example below:class Customer { private $name; private $credit_card_number; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setCC($cc) { $this->credit_card_number = $cc;
    • } public function getCC() { return $this->credit_card_number; } public function __sleep() { return array("name"); //because of this, only name isserialized }}$c = new Customer();$c->setName("Sunil");$c->setCC("1234567890123456");$data = serialize($c)."n";echo $data."n";Output:O:8:”Customer”:1:{s:14:” Customer name”;s:5:”Sunil”;}In the above example, you can see that the serialized string data only contains the name of theCustomer Object. This is because the __sleep() magic method returned an array containing onlythe ‘name’ data member.Working with the magic method __wakeup()__wakeup() magic method is the opposite of the __sleep() method. It is called when the objectof a class is about to be unserialized. This magic method __wakeup() does not accept anyparameter nor returns anything. The __wakeup() method is responsible for setup operations afteran object has been unserialized. Look at the example below:class Customer { private $name; private $credit_card_number; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setCC($cc) { $this->credit_card_number = $cc; } public function getCC() { return $this->credit_card_number; }
    • public function __sleep() { return array("name"); } public function __wakeup() { if($this->name == "Sunil") { //you would ideally fetch CC data from Database $this->credit_card_number = "1234567890123456"; } }}$c = new Customer();$c->setName("Sunil");$c->setCC("1234567890123456");$data = serialize($c)."n";var_dump(unserialize($data));Output:object(Customer)#2 (2) {[”name:private”]=>string(5) “Sunil”[”credit_card_number:private”]=>string(16) “1234567890123456″}In the above example, you can see that after the $c object has been serialized and the outputstored in $data variable, we use the $data variable and pass it to the unserialize(). Before theobject is unserizlied and object created, the __wakeup() method is called. In the __wakeup()method you should ideally make a database call to fetch data of the missing member variable.To clone an object means to create a duplicate of an object. With regular variables $a = $bmeans that a new variable $a gets created that contains the value of $b. This means that 2variables get created.With objects $obj2 = $obj1 does not mean that a new object i.e. $obj2 gets created. When weexecute $obj2 = $obj1, the reference of $obj1 is assigned to $obj2. This means that $obj1 and$obj2 point to the same memory space. Look at the diagram below.
    • Lets look at an example where only references are assigned to another object:class Customer { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; }}$c1 = new Customer();$c1->setName("Sunil");$c2 = $c1; //only reference or memory assigned to $c2$c2->setName("Vishal");echo $c1->getName()."n";echo $c2->getName()."n";Output:VishalVishalIn the above example, $c2 has the reference of $c1; therefore, when you set a new name in the$c2 object - $c1 object changes as well. Therefore, when an object is assigned as a reference;changes made to one object are also reflected in the other.Therefore, to create a new $obj2 object we must clone an object to create a new one. To clone anPHP5 Object a special keyword i.e. clone is used. Example below: $obj2 = clone $obj1;After the above line is executed $obj2 with a new memory space is created with the datamembers having the same value as that of $obj1. This is also referred to as shallow copy.
    • The above technique works with a class having data members that are of intrinsic type i.e. int,boolean, string, float, etc.. However, this technique will not work with a class that has a datamember which is an object of another class. In such a scenario, the cloned object continues toshare the reference of the data member object of the class that was cloned.So, how do we resolve this issue? Doing a regular shallow copy won’t help us. To allowaggregated objects (i.e. data members that are objects of another class) to also get clonedproperly we need to use the concept of ‘deep copy‘ as opposed to ‘shallow copy‘. To implementa ‘deep copy‘ you should implement the magic method __clone().You could also provide the implementation of __clone() magic method even when you don’thave an aggregated object. You would want to do this for providing necessary clean upoperations, conversions or validations.Lets explore a very simple example of cloning intrinsic data types:class Customer { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function __clone() { $c = new Customer(); $c->setName($this->name); return $c; }}$c1 = new Customer();$c1->setName("Sunil");$c2 = clone $c1; //new object $c2 created$c2->setName("Vishal");echo $c1->getName()."n";echo $c2->getName()."n";Output:SunilVishalIn the above example, observe the line where the statement $c2 = clone $c1 is executed. This isinternally represented as $c2 = $c1.__clone(). However, you cannot explicitly call the __clone()
    • method on an object as the __clone() is automatically called. Now that $c1 and $c2 are twoindividual objects, changes made to one object is not reflected in the other.Cloning aggregate objects (i.e. data members that are objects of another class)To clone a class having aggregated objects, you should perform ‘deep copy‘. Please refer to theexample below:class Order { private $order_id; private $customer; public function setOrderId($order_id) { $this->order_id = $order_id; } public function getOrderId() { return $this->order_id; } public function setCustomer(Customer $customer) { $this->customer = clone $customer; } public function getCustomer() { return $this->customer; } public function __clone() { $o = new Order(); $o->setOrderId($this->order_id); //force a copy of the same object to itself, otherwise //it takes the same instance. Seems like a bug to me $this->customer = clone $this->customer; $o->setCustomer($this->customer); return $o; }}class Customer { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function __clone() {
    • $c = new Customer(); $c->setName($this->name); return $c; }}$c = new Customer();$c->setName("Sunil");$o1 = new Order();$o1->setOrderId("OD0001");$o1->setCustomer($c);$o2 = clone $o1;$o2->getCustomer()->setName("Vishal");var_dump($c);var_dump($o1);var_dump($o2);Output:object(Customer)#1 (1) {[”name:private”]=>string(5) “Sunil”}object(Order)#2 (2) {[”order_id:private”]=>string(6) “OD0001″[”customer:private”]=>object(Customer)#3 (1) {[”name:private”]=>string(5) “Sunil”}}object(Order)#4 (2) {[”order_id:private”]=>string(6) “OD0001″[”customer:private”]=>object(Customer)#6 (1) {[”name:private”]=>string(6) “Vishal”}}In the above example both $o1 and $o2 have their own set of customer objects, thereforechanges made to one object is not reflected in another. This example implements the concepts of‘deep copy‘.
    • A special note on $this->customer = clone $this->customer; For some reason it is necessary to dothis for proper working of aggregated cloning.