Advantage / disadvantage of private variables?

I use almost all of my private class variables and create “wrappers” that get / set them:

class Something{ private $var; function getVar(){ $return $this->var; } } $smth = new Something(); echo $smth->getVar(); 

I see that many people do this, so I ended up doing the same :)

Is there any advantage to using them in this way compared to:

 class Something{ public $var; } $smth = new Something(); echp $smth->var; 

?

I know that private means that you cannot access them directly outside the class, but for me it is not very important if the variable is accessible from anywhere ...

So is there any other hidden advantage that I am missing with private variables?

+7
source share
5 answers

It differs from case to case if you want to use private variables with public getters and setters or just want to declare the variable as public.

The reason may be useful to use "getters" and "seters" if you want to have control over when someone gets access to the data.

As an example, let's say you got the following:

 public setBirthday($date) 

You can then verify that the date passed to this installer is a valid date of birth.

But you cannot, if you simply declare the variable as public.

 public $birthday; 

Based on the comments.

In addition, if you decide to change the internal storage mechanism to a string containing the number of seconds date from 01/01/1970, you can still represent the date from the outside in the same way if you use encapsulation, but not if you set the variables directly. Every piece of code that touched an internal variable right now will be broken.

This means that if the internal storage engine changes by the number of seconds from 1/1/1970, you do not have to change the external API. The reason is because you are in full control of it:

 public getBirthday() { // you can still return a string formatted date, even though your // private variable contains number of seconds from 1/1/1970 } 
+3
source

It is called encapsulation , and it is very good. Encapsulation isolates your class from other classes, avoiding it with internal elements, they only get access through your methods. It also protects them from changes that can be made to the class. Without encapsulation, if you make changes to the name or use of a variable, this change applies to all other classes that use the variable. If you force them to pass a method, there is at least a chance that you can handle the change in the method and protect other classes from changing.

+4
source

Access modifiers do not make much sense in scripting languages. Many real object oriented languages ​​like Python or Javascript don't have them.

And the prevalence of naive getter / setter methods is because PHP does not provide an explicit construct for this. http://berryllium.nl/2011/02/getters-and-setters-evil-or-necessary-evil/

+3
source

This is for discrete variables that are internal to the implementation of the class, from variables intended for external changes. There are also protected variables that are used internally and use extensions for the class.

We make private variables so that only code within the class can modify the variables, protecting external interference, guaranteeing control and the expected behavior of the variable.

+2
source

The purpose of encapsulation is to hide the internal objects of an object from other objects. The idea is that the outer trace of an object is a certain type, think of it as a contract with other objects. Inside, you may have to jump over some hoops to provide functionality that goes out, but does not apply to other objects. They should not be able to mess with him.

For example, let's say you have a class that provides sales tax calculations. Some kind of utility facility, basically. It has several methods that provide the necessary functionality.

Internally, this class beats the database to get some values ​​(for example, tax for a given jurisdiction) for performing calculations. It may support a database connection and other database related stuff, but other classes don't need to know about it. Other classes are only associated with a functionality contract that goes out.

Suppose that after some time the database should be replaced by an external web service. (The company intends to use the service to calculate sales tax, and not support it domestically.). Since the class is encapsulated, you can change its internal implementation to use the service instead of the database very easily. The class simply must continue to provide the same functions as the external ones.

If other classes deal with inner classes, then reimplementation will risk other components of the system.

+2
source

All Articles