In object-oriented programming, encapsulation is one of the most applied and necessary concepts for the proper development of the methodology within the development environment.

Private attributes need to, somehow, be accessed publicly. Thus, as a condition for the entry, modification and verification of values, we use the so-called accessor methods, also known as getters and setters.

 

get_set

GET is the nomenclature used in methods that return the value of the private variable; SET is already used in methods that indicate a new value to the variable.

In PHP, you can apply to the class both methods, GET and SET, thus ensuring the application of the concept of encapsulation, as can be seen in the example below:

 

class Employee{

private $name;

public function getName(){

return $this->$name;

}

public function setName($a){

$this->$name = $a;

}

}

In the example, we have the variable $name being encapsulated through the getName () and setName () methods, returning to and including values respectively.

If one needs to encapsulate an entire object, it’s also necessary to make use of accessor methods in order to ensure the implementation and application of the concept. Check following:

class Employee{

private $name;

private card = new Card();

 

public function getName(){

return $this->$name;

}

public function setName($a){

$this->$name = $a;

}

 

            public function getCard(){

return $this->$card;

}

 

public function setCard($ident, $rg, $sector){

$this->$card->setIdent($ident);

$this->$card->setRg($rg);

$this->$card->setSector($sector);

}

 

}

In this second example we have the practical application of the concept, in which are used the GET and SET methods of an object inside another object.

By , The most efficient web development environment. Create your web solutions in a fast and innovative way, reducing the development time in up to 80%. Access http://scriptcase.net and learn more.

June 4, 2015

a

You might also like…

“Scriptcase is the best IDE we have used!” (SUITE TECHNOLOGY)

See how Suite Technology used Scriptcase to develop their projects. OVERVIEW Suite Technolog...

Exceptional Services Bestowed by Scriptcase Gets Recognized at GoodFirms

When it comes to Web hosting services Scriptcase is a choice. If you ask why? It’s becaus...

IaaS, PaaS and SaaS: understanding Cloud Computing types.

Cloud Computing is a factor that has been responsible for causing several important transformations...

You might also like…

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.