Object oriented programming (OOP) in PHP

Object oriented programming (OOP) in PHP
5 (100%) 1 vote[s]

Advantages of OOP in PHP

Applications written using OOP are usually relatively easy to use and understand, recommended especially for large projects, where more developers can work.

  • Because applications are real world objects based, using OOP we can create classes, a direct mapping of things,
    with properties and behaviors as the real world concepts.
  • Another benefit of OOP is code reuse. We can create and use classes in different applications, making application writing more simple and efficient.
  • Another major OOP advantage comes from the modularity of classes. For example if you discover a bug or want to change the code, you have to change it in one place inside the class, without having to go to more places.

Basic OOP Concepts

At the base of oop programming are the classes, properties, and methods.

Classes

Classes are mapping of real world things. In the real world, objects have characteristics and behaviors. Let’s think of a vehicle, for example. A vehicle has a color, a weight, etc; those are its characteristics.
Also, a vehicle can go, stop, turn etc; Those are its behaviors.
Those characteristics are common to all vehicles, so using OOP You can create a class named “Vehicle”, to model all vehicles.

Objects

What is an object?

An object is a specific instance of a class. For example, if you created a Vehicle class, you can
create an object called myVehicle that belongs to the Vehicle class. You can create as many objects as you like.

If a class has general characteristics, an object has specific values associated with class attributes. For example, if Vehicle class have color as an attribute, the object myVehicle will have specified color, such as “black”.

How to create a class in PHP?

First you need to create the class before you create an object belonging to that class. To create a class, you use PHP’s class keyword.

class Vehicle{

}
A common coding standard is to begin the class name with a capital letter, though it is not mandatory.

How to create an object in PHP?

It’s very simple to create an object in php. To create an object use the new keyword, followed by the name of the class that you want the object to belong to.

$firstVehicle = new Vehicle();
$secondVehicle = new Vehicle();

Even both objects are in the same class, they are independent of each othe and each is stored in its own variable.

Objects and references

As in most programming languages, In PHP objects are always passed by reference! If you want to assign by value instead, use the clone keyword.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn’t contain the object itself as value anymore. It only contains an object identifier which allows to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object. Read more here about php objects and references.

For example at functions the arguments are passed default by value; that mean if the value of the argument within the function is changed, it does not get changed outside of the function. To allow a function to modify its arguments, they must be passed by reference (add & to the argument name in the function definition).

Properties

This characteristics of a class or object are known as its: properties. Properties are similar to variables because they have a name and a value.

Property Visibility

In PHP each property of a class can have one of three visibility levels:

  • Public property – can be accessed by any code, whether that code is inside or outside the class, from anywhere in your script
  • Private property – can be accessed only by code inside the class
  • Protected property – is slightly more permissive in the sense that it can be accessed by code inside the class and from any class that inherits the class.

How to declare and use properties inside a class?

To declare a property inside a class, first use the keyword for visibility level (public, private or protected), followed by the property’s name.
Also you can initialize properties at the time that you declare them, similar with variables.

!If the keyword for visibility is not specified, public is default.

To access a property, use the variable storing the object, followed by “->”, followed by the property name.

class Vehicle{
	public $color;
	public $type;
}
$firstOb = new Vehicle();
$firstOb->color = "red";
$firstOb->type = "Auto";
echo "My first vehicle is {$firstOb->type} and have {$firstOb->color} color";

//Result: My first vehicle is Auto and have red color

How to use and access static properties?

Static members of a class are independent of any particular object derived from that class. To create a static class
properties, just add static keyword before property name.

To access a static property use class name, followed by :: operator, followed by the property name.

How to use and access constants

To define a class constant, use the keyword cons. It’s a good practice to use all capitalized letters for the constant name.
To access a class constant use class name, followed by :: operator, followed constant name.

class Vehicle{
	public static $first_car_author = "Karl Benz";
	const FIRST_CAR_YEAR = 1885;
}
echo 'First car was made in '.Vehicle::FIRST_CAR_YEAR.' by '.Vehicle::$first_car_author.' from Germany';

//Result: First car was made in 1885 by Karl Benz from Germany

Methods

The behaviors of a class are known as its: methods . Methods are similar to functions in php; in fact, methods are defined usings the function statement.

The methods of a class, along with its properties, are known as members of the class.

Methods Visibility

In PHP each method of a class can have one of three visibility levels:

  • Public methods – can be called by any code, whether that code is inside or outside the class, from anywhere in your script
  • Private methods – can be called only by code inside the class
  • Protected methods – protected method can be called by other methods in the class, or in a class that inherits from the class.

How to create and use methods inside a class?

To create a method inside a class, first use the keyword for visibility level (public, private or protected), then the function keyword followed by the method name, followed by parentheses. The method code is inside curly braces.

!If the keyword for visibility is not specified, public is default.

To call a method, use the variable storing the object, followed by “->”, followed by the method name.

Leave a Reply

Your email address will not be published. Required fields are marked *