P. 1


|Views: 121|Likes:
Published by Ameengs

More info:

Published by: Ameengs on Jul 22, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less






  • Example of a Class:
  • Definition of an Object
  • Creating Objects in PHP5 Class
  • Example Code:
  • Objects as Attributes
  • Definition of Access Specifiers
  • Why do we need Access specifiers
  • Explanation of each access specifier
  • 1. Private
  • 3. Protected
  • Definition of an class method
  • Parameterized Constructor or Argument Constructor
  • Example:
  • Important Note:
  • Accessor Methods:
  • Mutator Methods:
  • Lets look at an example of accessor and mutator methods below:
  • Output:
  • Behaviour of instanceOf operator in inheritance
  • Example of accessing Constants within a function
  • Some observations on Constants
  • Definition of Inheritance:
  • PHP5 Inheritance
  • Inheritance and Access Specifiers
  • 1. private
  • 2. public
  • 3. protected
  • PHP5 Inheritance - Method Overriding
  • Definition of Method Overriding:
  • Lets look at an example with Bird:
  • Another example of function overriding in Inheritance
  • PHP5 Inheritance - Invoking parent methods
  • PHP5 Inheritance - Invoking parent Constructor and Destructor
  • Example of Abstract Class
  • Abstract Classes
  • Interface
  • Lets look at an example:
  • Tip:
  • Look at the example below:
  • Working with the magic method __sleep()
  • Working with the magic method __wakeup()
  • Cloning aggregate objects (i.e. data members that are objects of another class)

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 interface

Meaning of Polymorphism

Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning
forms). Polymorphism means many forms. In C you have two methods with the same name that
have different function signatures and hence by passing the correct function signature you can
invoke 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 the
parameters being passed to it.

The meaning with Object Oriented languages changes. With Object Oriented language
polymorphism happens:

When the decision to invoke a function call is made by inspecting the object at runtime it is
called Polymorphism

Why method polymorphism cannot be achieved

The reason why polymorphism for methods is not possible in PHP is because you can have a
method that accepts two parameters and call it by passing three parameters. This is because PHP
is not strict and contains methods like func_num_args() and func_get_arg() to find the number of
arguments passed and get a particular parameter.

Because PHP is not type strict and allows variable arguments, this is why method polymorphism
is not possible.

PHP 5 Polymorphism

Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class
methods. The basis of polymorphism is Inheritance and overridden methods.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->