New Object-Oriented Features in php 5



The following list provides the main new features:
   
public/private/protected:
Access modifiers for methods and properties. Allows the use of common OO access modifiers to control access to
methods and properties:
class MyClass {
private $id = 18;
public function getId() {   
return $this->id;
}
Unified constructor name  __construct():
Instead of the constructor being the name of the class, it is now declared as __construct() , which makes it easier to shift classes inside class hierarchies:
class MyClass {
                function __construct() {
                                print "Inside constructor";
                }
}
Object destructor support by defining a __destructor() method:

Allows defining a destructor function that runs when an object is destroyed:
class MyClass {
function __destruct() {
                print ”Destroying object”;
}
}

Interfaces:

Gives the ability for a class to fulfill more than one is-a relationships. A class can inherit only from one class, but  may implement as many interfaces as it wants:

interface Display {
                function display();
}
class Circle implements Display {
                function display() {
                                print "Displaying circle\n";
                }
}

“Instanceof” operator.  
Language-level support for is-a relationship checking. The PHP 4 is_a() function is now deprecated:

if ($obj instanceof Circle) { 
                print '$obj is a Circle';
}
  
Final methods.
The final” keyword allows you to mark methods so that an inheriting class cannot overload them:
class MyClass {
                final function getBaseClassName() {
                                return __CLASS__;
                }
}

Final classes.
After declaring a class as “final” , it cannot be inherited. The following example would error out.
final class FinalClass {
}
class BogusClass extends FinalClass {
}

Explicit object cloning.
To clone an object, you must use the clone keyword. You may declare a __clone() method, which will be called during the clone process (after the properties have been copied from the original object):

class MyClass {
function __clone() {
                print "Object is being cloned";
}
}
                $obj = new MyClass();
                $obj_copy = clone $obj;

Class constants.
Class definitions can now include constant values and are referenced using the class:
class MyClass {
                const SUCCESS = "Success";
                const FAILURE = "Failure";
}
                print MyClass::SUCCESS;  

Static methods.
You can now define methods as static by allowing them to be called from non-object context. Static methods do not define the $this variable because they are not bound to any specific object:
class MyClass {
static function helloWorld() {
                print "Hello, world";
}
}
MyClass::helloWorld();

Static members.
Class definitions can now include static members (properties) that are accessible via the class. Common usage of static members is in the Singleton pattern:
class Singleton {  
static private $instance = NULL;
private function __construct() {
}
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
Abstract classes.
A class may be declared “abstract” to prevent it from being instantiated. However, you may inherit from an abstract class:
abstract class MyBaseClass {
function display() {
                print "Default display routine being called";
}
}


Abstract methods.
A method may be declared abstract , thereby deferring its definition to an inheriting class. A class that includes abstract methods must be declared abstract :
abstract class MyBaseClass {
abstract function display();
}

Class type hints.
Function declarations may include class type hints for their parameters. If the functions are called with an incorrect class type, an error occurs:
function expectsMyClass(MyClass $obj) {
}

Support for dereferencing objects that are returned from methods.
In PHP 4, you could not directly dereference objects that were returned from methods. You had to first assign the object to a dummy variable and then dereference it.
PHP 4:
$dummy = $obj->method();  
$dummy->method2();
PHP 5:
$obj->method()->method2();

Iterators.
PHP 5 allows both PHP classes and PHP extension classes to implement an Iterator interface. After you implement this interface, you can iterate instances of the class by using the foreach() language construct:
$obj = new MyIteratorImplementation();
                foreach ($obj as $value) {
                                print "$value";
                }

__autoload().

Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances is having to write a long list of needed inclusions at the beginning of each script (one for
each class). In PHP 5, this is no longer necessary. You may define an __autoload() function that is automatically called in case you are trying to use a class that has not been defined yet. By calling this function, the scripting
engine offers one last chance to load the class before PHP bails out with an error:
function __autoload($class_name) {
                include_once($class_name . "php");
}   
                $obj = new MyClass1();
                $obj2 = new MyClass2();
}

Comments

Popular posts from this blog

PHP - How to increase size of POST value in php

D8 KernelEvents constants

D8 - Attach file programmatically to a node