Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.

The class name can be any valid label which is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

Published in: Technology
  • Be the first to comment

  • Be the first to like this


  1. 1. New Object-Oriented Features☞ public/private/protected accessmodifiers for methods and properties.Allows the use of common OO accessmodifiers to control access tomethods and properties:class MyClass {private $id = 18;public function getId() {return $this->id;}}
  2. 2. ☞ Unified constructor name__construct().Instead of the constructor being thename of the class, it is now declaredas __construct(), which makes iteasier to shift classes inside classhierarchies:class MyClass {function __construct() {print "Inside constructor";}}
  3. 3. ☞ Object destructor support bydefining a __destructor() method.Allows defining a destructor functionthat runs when an objectis destroyed:class MyClass {function __destruct() {print ”Destroying object”;}}
  4. 4. ☞ Interfaces.Gives the ability for a class to fulfill morethan one is-a relationships. A class caninherit only from one class, but mayimplement as many interfaces as it wants:interface Display {function display();}class Circle implements Display {function display() {print "Displaying circlen";}}
  5. 5. ☞ instanceof operator.Language-level support for is-arelationship checking. The PHP 4is_a() functionis now deprecated:if ($obj instanceof Circle) {print $obj is a Circle;}
  6. 6. ☞ Final methods.The final keyword allows you to markmethods so that an inheriting classcannot overloadthem:class MyClass {final function getBaseClassName() {return __CLASS__;}}
  7. 7. ☞ Final classes.After declaring a class as final, itcannot be inherited. The followingexamplewould error class FinalClass {}class BogusClass extends FinalClass {}
  8. 8. ☞ Static methods.You can now define methods as static byallowing them to be called fromnon-object context. Static methods do notdefine the $this variablebecause they are not bound to any specificobject:class MyClass {static function helloWorld() {print "Hello, world";}}MyClass::helloWorld();
  9. 9. • ☞ Abstract classes.• A class may be declared abstract to prevent itfrom being instantiated.• However, you may inherit from an abstract class:• abstract class MyBaseClass {• function display() {• print "Default display routine being called";• }• }