Classes in PHP




PHP has classes for the purpose of abstraction and encapsulation. Class is a collection of variables and functions which performs similar task. Classes help in security issues by separating interface from implementation.

These add more restriction to functions and variables so that other can not access them easily. Class is defined with class keyword following with its name. For code standard purposes, class name starts with a capital letter. A simple syntax of class is:

<?php 
 class ClassName
 {
   
 } 
?>

The code inside the class can not be executed directly. First we have to make object of that class. Object is instance of that class which has all variables and functions of that class as its properties.

We create an object like this: object_name = ClassName();

For calling a function of class, pointer '->' is used. (object_name->method_name();)

<?php

 class ClassName
 {
  // defining class variable
  // can only assign simple values during initialisation
  var $class_variable;

  //constructor of the class
  # equivalent to => function __construct(){}
  function ClassName()
  {
   $this->class_variable = 60 * 60;
   echo "it is constructor <br>";
  }

  // returning class variable
  function get_global()
  {
   return $this->class_variable;
  }

  // modifying class variable
  function set_global($value)
  {
   $this->class_variable = $value;
  }

  // displaying class variable
  public function reset_display()
  {
   $this->private_function();
   echo $this->get_global()." <br>";
  }

  // private function
  private function private_function()
  {
   $this->class_variable = 60 * 60;
  }

 } 

 $object_name = new ClassName();
 echo $object_name->get_global()."<br>";
 $object_name->set_global(231);
 echo $object_name->get_global()."<br>";
 $object_name->reset_display();
?>


In above example, you can see following concepts:

  • A variable which is accessible in whole class ($class_variable) is defined with keyword 'var'. Local variables are defined inside the functions and can be accessed only from those functions.
  • Class variables can only assign constant values (2, 4, 'string', etc) during the definition time. We can not define a class variable like this => var $variable = 60 *60; because 60*60 is a mathematics expression, not a constant.
  • Class variable and functions are accessed in the class using '$this->variable_name;' and '$this->function_name();' respectively. $this is the pointer of the same class.
  • Constructor is a special function in the class. It is used to initialize class variables. This function run when we create object of that class. We can also send arguments to the constructor.
  • We can define a constructor in two ways: first by giving it name same as class name and second by giving it name as __construct.
  • There are three types of access identifiers: public, private and protected. By default functions of the class are public. Public keyword means method is open for whole world and who can access this class, can access that function also. Private means that function is accessible only inside the class and no one can access that function from outside that class. Protected means that function can be access only from that class and classes extending that class. It is concept of inheritance which we will read in next tutorial.

Output of above code is:

it is constructor 
3600
231
3600



Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.