×
☰ See All Chapters

PHP OOP - Classes and Objects

Classes and Objects are used to make your code more efficient and less repetitive by grouping similar tasks. A class is used to define the actions and data structure used to build objects. The objects are then built using this predefined structure.

Defining a class

Syntax

class ClassName {

    // properties

    // Functions

}

All the members of class except construct and destruct functions should have access specifiers. You can learn about construct and destruct functions in next chapter PHP construct and destruct.  Scroll below to read about access specifiers.

Class Example

<?php

class Box {

   public $width;

   public $height;

   public $depth;

   

   public function volume () {

       return $width * $height * $depth;

   }

}

?>  

Creating objects

Object is an instance of a class. You can think of a building which is an instance of its blueprint. A running application is an instance of its code. Same way object is an instance of class. Classes are nothing without objects! You can create any number objects from a class. Each object has all the properties and methods defined in the class, and can have different property values.

Objects of a class is created using the new keyword.

Syntax to create objects

$object_reference_variable = new ClassName();

Syntax to access class members

$object_reference_variable->membername;

If member is a variable, it should be used without $ symbol.

Example

<?php

 

class Box {

   public $width;

   public $height;

   public $depth;

   public $mass;

   

   public function volume () {

       return  $this->width *  $this->height *  $this->depth;

   }

   public function density () {

     return  $this->volume () * $this->mass;

   }

}

 

$box = new Box();

$box->width = 10;

$box->height = 20;

$box->depth = 30;

 

echo "Volume of Box: ". $box->volume() . "</br>";

echo "Density of Box: ". $box->density();

 

?>  

this keyword

  • this is a keyword in PHP. 

  • this is a reference variable which refers to the current object. 

  • this keyword is used to access the members of the class within the same class. Accessing any member of a class within the class can be done only using this. 

PHP- Access Specifiers

Access specifiers specify who can access them. There are three access specifiers used in PHP.

public - the property or function can be accessed from everywhere. This is default.

protected - the property or function can be accessed within the class and by classes derived from that class.

private - the property or function can only be accessed within the class.

<?php

 

class Box {

   public $width;

   protected $height;

   private $depth;

   

   protected function volume () {

       return  $this->width *  $this->height *  $this->depth;

   }

   

}

 

$box = new Box();

$box->width = 10; // OK

$box->height = 20; // ERROR

$box->depth = 30; // ERROR

$box->volume();// ERROR

 

class BoxWeight extends Box {

    public $mass;

   

    public function density () {

        echo "can access protected member height: ". $this->height;

        echo "cannot access private member depth: ". $this->depth; //Error

        return  $this->volume () * $this->mass;

    }

}

 

?>  

PHP Namespaces

Sometime you have to use same class name for different classes. As an example you have to use name Table for HTML table element and use name Table for furniture table. In such cases you can use namespaces support. Using namespace you can use same name for different classes, but classes should be in different namespace scope.

Syntax to declare namespace

Namespaces should be declared at the beginning of a file using the namespace keyword:

namespace nameOfNamespace;

Syntax to use namespace

Whenever your class name is conflicting with the other class which is having same name, you can attach namespace to class name. Namespace should be attached to class name with the below syntax:

namespaceName\className();

PHP Namespaces Example

furnituretable.php

<?php

namespace furniture;

 

class Table {

    public function printType ( ) {

        echo "I am furniture table </br>";

    }

}

 

?>

htmltable.php

<?php

namespace Html;

 

class Table {

    public function printType ( ) {

        echo "I am HTML table </br>";

    }

}

 

?>

index.php

<?php

include "furnituretable.php";

include "htmltable.php";

 

$table1 = new Html\Table();

$table2 = new furniture\Table;

 

$table1->printType();

$table2->printType();

?>

Project Directory Structure

php-oop-classes-objects-0

Output

php-oop-classes-objects-1


All Chapters
Author