The concept of encapsulation is relatively simple, based on the following: a variable needs to be protected within a class, so that the change of its value does not interfere negatively in other ways within the system. Let’s go to a practical example to make it easier to understand what this means.

oop_encapsulation

For security reasons, in a traffic control system, the speed variable, inside the car class, you can not reach or exceed the value 120. For this security measure to be guaranteed, the concept of encapsulation comes into play.

class Car{

private $speed;

function valueSpeed($spe){

If ($vel < 120)

{

$speed = $spe;

}

}

}

 

In class Car, the variable $speed, which controls the car’s speed itself, is set as private – so only the context of the class can access it. For an object to be able to manipulate it, it’s necessary to use the valueSpeed () method that gets the value to be set in the variable and makes the necessary verifications.

$car = new Car;

$car->valueSpeed(111) // variable $speed takes the value 111

$car-> valueSpeed(120) // variable $ speed continues with the value 111

$car-> valueSpeed (109) // variable $ speed takes the value 109

$car->$speed = 115 // System error as the variable is private.

 

The concept of encapsulation is vital for the control of a system since it ensures that all the validations are made before a given variable value or other kind of system information is changed – thus ensuring that possible errors in the application business rules happen.

You might also like…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.