Wednesday, February 18, 2009

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.
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
Method polymorphism cannot be achieved in PHP :
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.
Eg:

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 the processClass() method. The parameter accepted in processClass() is that of BassClass. Within the processClass() the method myMethod() is being called. As per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.

Final Class and Methods
Final Class :

A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’.
Eg:
final class BaseClass {
public function myMethod() {
echo "BaseClass method called";
}
}

//this will cause Compile error
class DerivedClass extends BaseClass {
public function myMethod() {
echo "DerivedClass method called";
}
}

$c = new DerivedClass();
$c->myMethod();

In the above example, BaseClass is declared as final and hence cannot be extended (inherited). DerivedClass tries to extend from BaseClass and hence the compiler will throw a compile error.
Final Method :
A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the ‘final’ keyword.
Eg:
class BaseClass {
final public function myMethod() {
echo "BaseClass method called";
}
}

class DerivedClass extends BaseClass {
//this will cause Compile error
public function myMethod() {
echo "DerivedClass method called";
}
}

$c = new DerivedClass();
$c->myMethod();
In the above example, DerivedClass extends from BaseClass. BaseClass has the method myMethod() declared as final and this cannot be overridden. In this case the compiler causes a compile error.

Static Data Member :
A data member that is commonly available to all objects of a class is called a static member. static members share the memory space between all objects of the same class.

Defining static data members in PHP5
To define a static member in PHP5 you need to prefix the class member name with the keyword ’static’.
Eg:
class Customer {

private $first_name; // regular member
static public $instance_count; //static data member

}
Accessing static data members in PHP5
A static member data can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class
Eg:
class Customer {

static public $instance_count = 0; //static data member

public function __construct() {
Customer::$instance_count++;
}

public function __destruct() {
Customer::$instance_count--;
}

public function getFirstName() {
//body of method
}

static public function getInstanceCount() {
//body of method
}
}

$c1 = new Customer();
$c2 = new Customer();

echo Customer::$instance_count;

Output:
2
In the above example, $instance_count is a static data member. Every time a new object is created the constructor is executed and the $instance_count variable is incremented by one. To echo the value contained in $instance_count variable, we use the :: (scope resolution) operator.

Static Method :
A static method is a class method that can be called without creating an instance of a class. Such methods are useful when creating utility classes.
Defining static methods in PHP5
To define a static data methods in PHP5 you need to prefix the class method name with the keyword ’static’.
Eg:
class Customer {

public function getFirstName() {
//body of method
}

static public function getInstanceCount() {
//body of method
}
}
Accessing static method in PHP5
A static method can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class. However, you can also access it with an instance variable.
Eg:

class Customer {

static public $instance_count = 0; //static data member

public function __construct() {
Customer::$instance_count++;
}

public function __destruct() {
Customer::$instance_count--;
}

public function getFirstName() {
//body of method
}

static public function getInstanceCount() {
return Customer::$instance_count;
}
}

$c1 = new Customer();
$c2 = new Customer();

echo Customer::getInstanceCount(); //this is using the scope resolution operator
echo $c1->getInstanceCount(); //this is using the instance variable
Output:
2
2
Rules to keep in mind for static methods

  • A static method can only access static data members

  • A static method does not have access to the $this variable

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home