PHP

Creating Classes in PHP

This article explains Creating Classes in PHP.

Basically, a class is like a template or a blueprint for an object. In other words, a class describes an object. While objects are things that have certain features in terms of attributes and behavior. The class represents the attributes and behavior of objects and the objects are the instances of the class. The attributes of the object are declared as the data members and the behavior is defined using the member functions.

The syntax for Creating Classes in PHP

In order to create a class, we use the class keyword followed by the name of the class. Within the definition of class, we can create data members and member functions.

class <classname>
{
    //data members
    //methods
}

A class can also contain a special function that initializes the data members of the class. Basically, this special function is a constructor and we create it with the name __construct(). Similarly, we can also have a destructor. The destructor deletes an object by releasing the memory consumed by the object. Like a constructor, a destructor is also defined using the name, __destruct().

The following PHP script shows a class called MyClass. This class has two data members, a method, and a constructor. Further, we create data members using an access modifier. Basically, an access modifier is a keyword that determines where the data members or member functions can be accessed. There are three access modifiers in PHP – public, protected, and private.

The following code shows the use of public and private access modifiers. The public data member a is accessible within the class in the show() method as well as outside the class. So, we can access the public data member a through the object of the class. However, the private data member b is accessible only within the class in the show method. When we try to access it through the object outside the class, it results in an error.

<?php
  class MyClass
  {
     public $a;
     private $b;
     function __construct($a, $b)
     {
       $this->a=$a;
       $this->b=$b;
     }
     function show()
     {
	echo 'a = ',$this->a,'<br>b = ',$this->b;
     }
   }
   $ob=new MyClass(4, 5);
   $ob->show();
   echo '<br>a = ',$ob->a;
   echo '<br>b = ',$ob->b;    

?>

Output

The Example of Creating Classes in PHP
The Example of Creating Classes in PHP

Creating a Sub Class in PHP

The following example demonstrates inheriting a class in PHP. We need to use the extends keyword in order to create a sub-class.

<?php
  class A
  {
     public $a;
     function __construct()
     {
       echo 'Inside Base Class Constructor...<br>';
       $this->a=1000;
     }
     function show()
     {
	echo 'Inside function show()....<br>';
	echo 'a = ',$this->a;
     }
   }
   class B extends A
   {
     public $b;
     function __construct()
     {
       parent::__construct();
       echo 'Inside Derived Class Constructor...<br>';
       $this->b=2000;
     }
     function show_derived()
     {
	echo '<br>Inside function show_derived()....<br>';
	echo 'b = ',$this->b;
     }
   }
   $ob=new B();
   $ob->show();
   $ob->show_derived();  
?>

Output

Example of Inheritance in PHP
Example of Inheritance in PHP

Another example of Creating Classes in PHP is shown below.


Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

You may also like...