How important is visibility in PHP classes and why?

As you know, the PHP class has private, public and secure keywords. I just started writing classes, and I wonder what are the benefits of class visibility in PHP5.

And, of course, also disadvantages ...

+4
source share
5 answers

Useful if you want to apply OOP methods such as Encapsulation Information .

If you declare class members as private , they cannot be accessed from code outside your class. You must provide access methods to them. This separates the interface of your class from the actual implementation.
Another code that your class uses does not have to know the name of the class member or how you actually store the information inside.

Example:

Consider the Books class, somehow giving me a list of books. I can define a public member that contains an array of books:

 class Books { public $list; } 

On the other hand, if I define the getList() method, I can change the implementation later without executing the code that the class uses:

 class Books { private $list; public function getList() { // get list from database, from XML, etc. // might use $list internally but does not have to } } 

Obviously, you do not need modifiers such as private , protected or public to implement this behavior, but this leads to improved structured code and clearer interfaces. Imagine you have a class that has both public $list and the getList() method. How do you know which one to use?
This is used not only to get values, but especially to set values.

There are no drawbacks if you use them, only the benefits of IMHO. The difference between them is scope. public elements can be accessed from external code; protected elements can be accessed by the class inheritance form and private only class members.

Methods can also have these modifiers and follow a similar goal. For instance. if the method is declared as private , then it is clear that this is probably some kind of helper method that is used only internally and should not be called external.


So in the end it comes down to two things:

  • Control . Which parts of your class can be accessed, how
  • Self-documenting or understanding . Other people using your class can more easily determine which part of your class they should access and which should not.
+10
source

Visibility is one of the main ideas of encapsulation, which provides object-oriented programming (especially polymorphism). You must accurately determine visibility properly, as you cannot guarantee that your class will work as expected, otherwise. Using this correctly, you precisely control access to your class members.

I do not know any flaws (except that you need to write some additional lines of code).

+3
source

Let me answer an example:

Leaving all this to the client (not good):

 class myClass { public $name; // cannot contain spaces } $mc = new myClass(); $mc->name = 'John Smith'; // Oh no! 

Now with more control:

 class myClass { private $name; // cannot contain spaces public function setName($value) { if (strpos($value, ' ') !== false) { throw new Exception('Name contains space.'); } $this->name = $value; } public function getName() { return $this->name; } } $mc = new myClass(); try { $mc->setName('John Smith'); } catch (Exception $e) { echo 'Cannot set name. Reason: ', $e->getMessage(); } 

Using access specifiers allows you to better protect members / methods from misuse . There are no flaws, except for something more than learning.

+3
source

Imagine what happens if you have full access to the electrical installation in your home. How long does it take to burn a building or kill yourself?

Internal processes can be dangerous, so this installation provides some open interface. In this example, it would be a connector and a light switch. It's quite difficult to burn a house with a light switch, isn't it?

+1
source

Yes, they are important. Simply put, they do not allow your code to access things in the wrong places.

I'm not sure of any flaws other than declaring any protected or private, it could mean a little more work writing a getter and setter method.

0
source

All Articles