Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between public and private in php

Shulou Source: shulou.com Published: 2022-06-02 18:57:41 10月04日 Update

This article introduces the relevant knowledge of "what is the difference between public and private in php". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

If a class member is declared public, it can be accessed anywhere; if a class member is declared private, it can only be accessed by the class that defines it. 2, public is a public type, permissions are the largest, can be called internally, instance calls, etc.;private is a private type, only used in this class.

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

Public, private and protected are called access modifiers. Just like C++, PHP has three access modifiers, public, private, and protected. You can define the visibility of attributes, methods, or constants by prepending declarations with these keywords.

If a class member is declared public, it can be accessed anywhere.

Public permissions are the largest, can be called internally, instance calls, etc.

If a class member is declared protected, it can only be accessed within the class itself and within inheritance and parent classes.

protected is a protected type for calls to this class and inherited classes.

If a class member is declared private, it can only be accessed by the class that defines it.

Private is a private type and is used only in this class.

Examples:

From the example above, we can see that:

public: can be called internally to class, can be instantiated.

private: can be called within class, instantiation calls error.

protected: can be called within class, instantiation call error.

From the example above, we can see that:

public: public in test can be inherited.

private: private in test cannot be inherited.

protected: protected in test can be inherited.

static: static in test can be inherited.

"What is the difference between public and private in PHP" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Members instances methods types subclasses properties private maximum examples content only place more permissions knowledge faceted practical identical learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker Shulou Tech Info Microsoft Linux