Topic outline


      Syntax to create a class :

      class classname


        function operation1()

        {  }

        function operation2($param1, $param2)

        {  }

      We can  create operations by declaring functions within the class definition. The following code creates a class named classname with two operations that do nothing. The operation operation1() takes no parameters, and operation2() takes two parameters shown above.


      Most classes have a special type of operation called a constructor. A constructor is called when an object is created, and it also normally performs useful initialization tasks such as setting attributes to sensible starting values or creating other objects needed by this object.

      A constructor is declared in the same way as other operations, but has the special name __construct(). Although you can manually call the constructor, its main purpose is to be called automatically when an object is created. The following code declares a class with a constructor:

      class classname


        function __construct($param)


          echo "Constructor called with parameter ".$param."<br />";  }}

      PHP supports function overloading within classes, which means that you can provide more than one function with the same name and different numbers or types of parameters.


      The opposite of a constructor is a destructor. They allow you to have some functionality that will be executed just before a class is destroyed, which will occur automatically when all references to a class have been unset or fallen out of scope.

      Similar to the way constructors are named, the destructor for a class must be named __destruct(). Destructors cannot take parameters.


      After we have declared a class, we need to create an object—a particular individual that is a member of the class—to work with. This is also known as creating an instance of or instantiating a class. we create an object by using the new keyword. When you do so, you need to specify what class your object will be an instance of and provide any parameters required by the constructor.

      The following code declares a class called classname with a constructor and then creates two objects of type classname:

      class classname


        function __construct($param)  {

          echo "Constructor called with parameter ".$param."<br />";


      $a = new classname("First");

      $b = new classname("Second");


      Within a class, we have access to a special pointer called $this. If an attribute of your current class is called $attribute, you refer to it as $this->attribute when either setting or accessing the variable from an operation within the class.

      The following code demonstrates setting and accessing an attribute within a class:

      class classname

      {  public $attribute;

        function operation($param)

       {    $this->attribute = $param;

         echo $this->attribute;  }}


      PHP uses access modifiers. They control the visibility of attributes and methods, and are placed in front of attribute and method declarations. PHP supports the following three different access modifiers:


      The default option is public, meaning that if you do not specify an access modifier for an attribute or method, it will be public. Items that are public can be accessed from inside or outside the class.


      The private access modifier means that the marked item can be directly accessed only from inside the class. In most cases, we will want to make all class attributes private. we may also choose to make some methods private, for example, if they are utility functions for use inside the class only. 


       The protected access modifier means that the marked item can be accessed only from inside the class. It also exists in any subclasses; 

      The following sample code shows the use of the public and private access modifiers:

      class manners


        private $greeting = 'Hello';

        public function greet($name)  {

          echo "$this->greeting, $name";  }}

      Here, each class member is prefaced with an access modifier to show whether it is private or public. 


      To specify that one class is to be a subclass of another, you can use the extends keyword. The following code creates a class named B that inherits from some previously defined class named A:

      class B extends A

      {  public $attribute2;

        function operation2()

        {  }}

      If class A was declared as

      class A

      {  public $attribute1;

        function operation1()

       {  }}

      all the following accesses to operations and attributes of an object of type B would be valid:

      $b = new B();


      $b->attribute1 = 10;


      $b->attribute2 = 10;

      Note that because class B extends class A, we can refer to $b->operation1() and $b->attribute1, although they were declared in class A. As a subclass of A, B has all the same functionality and data. In addition, B has declared an attribute and an operation of its own.

      It is important to note that inheritance works in only one direction. The subclass or child inherits features from its parent or superclass, but the parent does not take on features of the child. This means that the last two lines in this code make no sense:

      $a = new A();


      $a->attribute1 = 10;


      $a->attribute2 = 10; 

      The class A does not have an operation2() or an attribute2.


      An attribute in the subclass a different default value to the same attribute in its superclass or to give an operation in the subclass different functionality to the same operation in its superclass. This action is called overriding. In the following example

      class A{

        public $attribute = 'default value';

        function operation()

        {    echo 'Something<br />';

         echo 'The value of $attribute is '. $this->attribute.'<br />';  }}

      class B extends A{

        public $attribute = 'different value';

        function operation()

        {    echo 'Something else<br />';

          echo 'The value of $attribute is '. $this->attribute.'<br />';  }}

      Declaring B does not affect the original definition of A. Now consider the following two lines of code:

      $a = new A();


      These lines create an object of type A and call its operation() function. This produces


      The value of $attribute is default value

      proving that creating B has not altered A. If you create an object of type B, you will get different output.

      This code

      $b = new B();



      Something else

      The value of $attribute is different value

      In the same way that providing new attributes or operations in a subclass does not affect the superclass, overriding attributes or operations in a subclass does not affect the superclass.