PHP Class: The Complete Guide

OOP is the programming style in which it is necessary to group all the variables and functions of a particular topic into a single class.

PHP Class

Class in PHP is a programmer-defined data type, including the local functions and the local data. You can think of a class as the template for making many instances of the same kind (or class) of the object.

A class is a constructor prototype from which objects are created.

The class defines constituent members, enabling the class instances to have state and behavior.

Data field members enable the class object to maintain the state, and methods allow the class object’s behavior.

Defining PHP Classes

See the general form of a PHP class.

<?php
   class phpClass {
      var $var1;
      var $var2 = "Game of Thrones";
      
      function myfunc ($arg1, $arg2) {
         // function body
      }
   }

Here is the description of each line.

  1. The particular form class is followed by the name of the class you want to define.
  2. The set of braces enclosing any number of variable declarations and function definitions.
  3. Variable declarations start with the particular form var, followed by the conventional $ variable name; they may also have the initial assignment to the constant value.
  4. Function definitions look much like the standalone PHP functions but are local to a class and will be used to set the object data and access the object data.

Creating classes and Instantiation

  1. The class definition starts with a keyword class followed by the class name, then followed by the set of curly braces ({}) which enclose the constants, variables (called “properties”), and functions (called “methods”) belonging to the class.
  2. The valid class name (excluding the reserved words) starts with the letter or underscore, followed by the number of letters, numbers, or underscores.
  3. Class names usually begin with the uppercase letter to distinguish them from other identifiers.
  4. The instance is the object that has been created from an existing class.
  5. Creating the object from an existing class is called instantiating the object.
  6. The new keyword must be used if we want to create an object out of a class.
  7. Classes should be defined before Instantiation.

How to add properties to a class

We call properties to the variables inside the class.

Like any other variable, properties can accept values like strings, integers, and booleans (true/false values). See the following code.

<?php
  class Actor {
      public $name = "KRUNAL";
      public $favshow = "Game of Thrones";
  }

The $name and $favshow are the properties.

We put a public keyword in front of the class property.

The naming convention is to start a property name with the lower case letter.

A property can have a default value. We can also create the property without a default value.

You can access the properties after you create an object of the defined class.

<?php
  class Actor {
      public $name = "KRUNAL";
      public $favshow = "Game of Thrones";
  }

  $leo = new Actor;
  echo $leo->name."\n";
  echo $leo->favshow;

See the output.

➜  pro php Actor.php
KRUNAL
Game of Thrones                                                                 
➜  pro

Defining Methods in Class

The functions which are declared in the class are called methods. The class method is precisely similar to the PHP functions.

Declaring a method in the class is an easy task; use one of a keyword public, protected, or private, followed by the method name.

  1. public: Public methods can be accessed from outside the class.
  2. private: It has no access granted from outside the class.
  3. protected: It has no access granted from outside a class except the class that’s the child of a class with a protected property or method.

A valid method name starts with a letter or underscores, followed by any number of letters, numbers, or underscores.

A method body is enclosed within a pair of braces that contains codes. The opening curly brace ( { ) indicates the beginning of a method code, and the closing curly ( } ) brace suggests the termination of the method.

The default is public if the public does not define the protected method or private.

You can access properties and methods of the current instance using $this (Format $this->property) for the nonstatic property. See the code.

<?php
  class Actor {
      public $name = "KRUNAL";
      public $favshow = "Game of Thrones";

      public function show() {
        return $this->favshow;
      }
  }

  $leo = new Actor;
  echo $leo->show();

See the output.

➜  pro php Actor.php
Game of Thrones                                                                 
➜  pro

We can approach the methods similar to the way we approach the properties, but we first need to create at least one object from a class.

In this tutorial, we have seen the world of object-oriented programming by learning about the classes and objects that can be created from them. 

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.