4 examples to learn PHP class, object, static and inheritence

4 examples to learn PHP class, object, static and inheritence

What is a class?

Grouping similar types of tasks into classes comes under the object-oriented programming. Where a Class is a combination of variables/properties and functions/methods.

This chapter does not aim at explaining object-oriented programming concepts thoroughly. The aim of this chapter is how to create and use PHP classes.

What is a PHP Class?

Class in PHP is a collection of variables and functions. Where functions work with these variables. A class, in general, can be taken as a template that is the basis of many instances in the form of objects.

See an example of a class

For example, a model of a to be launched new car can be taken as a class. Once the model is approved and cars made on the basis of that approved template or ‘class’ can be taken as objects.

In this tutorial, we will use the same example of car class to explain how classes can be created and used and different features of classes available in PHP.

How to define a class

A class can be defined as follows:

Let me explain it line by line:

class MyPHPClass

  • class keyword is used to define the class name, MyPHPClass, followed by curly braces {}.
  • You can declare local variables in a class.
  • All the functions within the class must be created by using keyword function. e.g.

Function myPhpfunc ($par1, $par2)

  • The function keyword is followed by yourFunctionName and in small brackets () that contain any arguments.
  • Variables are declared by using var keyword which is followed by typical variable name with a $ sign. e.g.

var $intvar1;

A class example in PHP program

Following example shows how to create a class. We will use the same car example as mentioned earlier.

Experience this example online

Understanding Classes and Objects

As explained in the above example, when a manufacturer of cars plans a new model. Before launching and bringing hundred of thousands of cars in the market, a model/prototype or template will be prepared and approved. Once approved, this can be the basis of manufacturing hundred of thousands of cars on the basis of that single model to bring in the market.

Now we can term that model or template as a PHP Class. Whereas all those cars made on that template basis are objects.

Just like that, a class is developed in Object Oriented Programming language like PHP (our current scope) that will be the basis of one or hundreds of objects using that class.

Creating Objects of a Class

Once a class is created or defined, you can use it as many times as you need by using the PHP object variables as follows:

$obj = new className;

$Objcar = new carnewModel;

Class Inheritance

When a class is created by ‘inheriting’ function(s) of another class then this process is called Inheritance.

The class being inherited is called as the parent class while the calling class is called Child class. The child class will inherit variables and one or more functions from the parent class.

Syntax of inheriting class

class Child_class_name extends Parent_class_name {

//Child class code: methods or properties

}

Extending our car example, let us say a car’s model (class) is approved and on that basis hundred of thousands of cars are in the market for sale. Now, the company has decided to give an added feature in a limited addition of that model. All of the features will remain the same in the limited addition plus one added feature.

So you don’t have to create a whole new model. Just ‘extend’ the existing one and add a new feature in it.

The carnewModel_limited_edition will inherit all methods and properties of the carnewModel parent class.

PHP Abstract function in Class

An abstract class in OOP is the one that can only be inherited and cannot be instantiated.

Class constructor

While working with the classes, there will be scenarios when you will need to initialize something whenever an object is created for a class. PHP provides a magical or special function for that purpose called the class constructor.

The Constructor function will be called automatically when an object is created for that class. You may set variable initial values there or perform other operation that does not require a function call.

Syntax of creating Class Constructor

function __construct( $par1, $par2 ){

//Constructor Code goes here

}

So you have to use __construct to create a special function. You can send as many arguments as you need to the constructor function.

In continuity to our above example, let us set the initial variable values in the __construct function below:

PHP class

Experience this example online

Output

The initial manufacturer price is: $15000

Initial color of car is: Black

As you can see, you only instantiated the carnewModel class and the constructor function executing echo commands automatically.

Using Static in a class

A function or property declared as Static in PHP class can be accessed without creating the class objects. You simply have to refer the class name followed by scope resolution operator and static function name or property (variables).

Syntax of Using Static

public static $staic_property = ‘Static test’;

public function static_func() {

return self::$staic_property;

}

An Example of using Static

PHP class static

Experience this example online

Scope of Properties and Functions in classes

There are three ways how you can declare methods and functions in classes; that defines how properties and functions are accessible.

These are as follows:

1- Public

Properties and functions declared as the public are accessible both within the class and outside.

2- Private

Property or methods declared as private are only accessible within the class that defines it.

e.g.

private function pri_function()

{

//private function code;

}

3- Protected

Properties or methods declared as protected means these are only accessible to the class that defines it or to child classes that extend it.

e.g.

protected function protected_func()

{

//Code goes here

}