2018/03/10 daily One learn PHP modifier public/private/protected

Source: Internet
Author: User
Tags learn php

For object-oriented

The use of modifiers is one of our most common and easily overlooked small details.

For programming, grasp every small detail, you can construct a beautiful, elegant program.

Public

Use the most modifier, public method, allow all access, just like a bus, come up to pay [parameter], money on the line, no matter who.

There is no more explanation for this modifier, because he is using too much.

PS: If a method does not have a modifier defined, the default is public in PHP5.

Private

  Private methods, which are not allowed to be accessed after direct instantiation, are not directly accessible by subclasses and are allowed only in their own classes.


I know the truth, I just want to ask, when should I use this modifier? Is it okay to use public? The rest of us can see it anyway.

1: When should I use this modifier, here I find a better understanding of the metaphor, I like this metaphor method.

Take a factory example, you as a factory owner, your patented technology, naturally do not want to be seen by others, so as not to be forged possible. So you decorate with private. Protect yourself. Then there is the public, something that doesn't matter, or can be copied or popularized by others, and other people can get it from you.

In general, methods that do not want to be exposed are implemented using private or protected methods. This also explains the encapsulation in object-oriented.

2: Is it not good to use public? The rest of us can see it anyway.

Here also found an answer, the specification is still very important.

If you develop a system to play on your own, that's fine. However, if you want to improve your technology, then it's necessarily about teamwork, or your system is open source for other people to use.

For example, your own development of the system, after a few months, you can not remember what was written, when you want to modify the code, you also need to be careful to see if this method can be called first, when the permissions are set, I know, this is definitely internal call, the outside will not tune.

There is another, if a class is to implement a very complex function, that method may have dozens of, a glance down, dazzling, it is difficult to understand their rights.

3: Part of the above answer is excerpted from

https://segmentfault.com/q/1010000008329419

protected

  be protected method, which is not allowed to be accessed after direct instantiation, can be accessed by subclasses and can be used in classes.

It's used quite like private, and the difference is that he can inherit.

Or take the factory example, your patent before is set to private, but as your factory slowly become larger, with a factory, your patent does not for the factory use? It's impossible. So protected is the solution to this problem.

  

For these three kinds of use, in later learning, if you feel something new, always Add.

2018/03/10 daily One learn PHP modifier public/private/protected

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.