PHP in public,private,protected,abstract and other keyword usage detailed

Source: Internet
Author: User

Keywords commonly used in PHP

There are a lot of keywords in PHP that restrict functions and classes, often abstract,final,interface,public,protected,private,static, and so on, and we'll analyze them for each usage.

keywords for variables and methods public,private,protected

Public has the most permissions, either for the subclass or for the instantiated calls.

Protected represents a protected, access permission that can be accessed only in subclasses and in this class

Private means that it can be accessed only in the current class.

<?PHP///** * Define MyClass*/classmyclass{ Public $public= ' Public '; protected $protected= ' Protected '; Private $private= ' Private ';  Public functionPrinthello () {Echo $this- Public; Echo $this-protected; Echo $this-Private; }    protected  functionpro_test () {Var_dump(1); }}$obj=NewMyClass ();Echo $obj- Public;//This line can be executed normally//echo $obj->protected;//This guild produces a fatal error//echo $obj->private;//This line can also produce a fatal error$obj->printhello ();//output public, Protected, and Private$obj->pro_test ();//Direct Error?>
Variable and method keyword static

The function of static is to be able to implement values or methods that are called without instantiation in the class, while static modified variables have the function of storing with values, for example, we do not use static to run the result as follows:

<? PHP function Test () {    $var=1;     Echo $var. " </br> ";     $var+ +;} Test (); // 1test (); // 1test (); // 1?>

If we add static to the variable, it's going to be like this .

<? PHP function Test () {    static$var=1;     Echo $var. " </br> ";     $var+ +;} Test (); // 1test (); // 2test (); // 3?>

This may not be able to appreciate the benefits of PHP, then we first assume that the reader is also familiar with JS, in JS is not static this keyword, so if we want to implement a program with the ability to save each time the results of the program as the basis of the next operation we need to write.

    var glo=0;     function Test () {        glo+ +;        Document.writeln (glo);    }    Test ();    Test ();    Test ();

This will leak the GLO into the global variable, if the resulting variable is more, it will lead to memory leak (memory leak refers to the variable takes up too much memory space does not release)

<script>    var glo=0;     function Test () {        glo+ +;        Document.writeln (glo);    }    Test (); // 1    Test (); // 2    Test (); //  </script>

So it has an advantage over not defining static in a language that holds variables, does not leak memory, and does not easily cause global variables to be misused (because scopes are not global)

$age=0;$age++;functiontest1 () {Static $age= 100; $age++; Echo $age." </br> ";}functiontest2 () {Static $age= 1000; $age++; Echo $age." </br> ";} Test1 (); 101test2 (); 1001

Keyword final for class and method

Final can only be used to modify class and function, using the final can not be inherited, such as the following code will directly error

 classBaseClass { Public $public= ' Public '; functionTest () {Echo"Baseclass::test () called\n"; }    Final  Public functionmoretesting () {Echo"Baseclass::moretesting () called\n"; }}classChildClassextendsBaseClass { Public functionmoretesting () {Echo"Childclass::moretesting () called\n"; }}
Special Keyword Interface,abstract

The significance of interface is to standardize the style of programming, imagine that if it is to implement an interface then we have to use this interface class when we have to implement the inside of the method, played the role of unified naming.

Class is a multi-inheritance interface, and the single inheritance between interfaces and interfaces is achieved through extends, and the relationship between class and interface is established by implements

Example code:

<?PHPInterfacetesta{functionFunca ();}Interfacetestb{functionFUNCB ();}InterfaceTestcextendsTestA {functionFUNCC ();}classRunImplementsTESTC,TESTB { Public functionFunca () {//todo:implement Funca () method.    }     Public  functionFUNCB () {//todo:implement FUNCB () method.    }     Public functionFUNCC () {//todo:implement FUNCC () method.    }}?>

Abstract has the same effect as interface, but all the methods in interface must be implemented, but in an abstract class, there can be one or more abstract modifiers, so we can understand , interface is a special case of abstract (when all methods are abstract methods, they must be implemented). Abstract has these characteristics as follows:

1. As long as there is at least one method in the class that uses the abstract keyword, the class is abstract, and the corresponding keyword is added

2. Abstract method, only the declaration part of the method, there is no method body.

But in my opinion, abstract has such a few scenes in practical applications.

1. Multi-person programming time to standardize the public part of the naming rules (do not do any instructions, the same principle as interface)

2. Implementation does not let the parent directly instantiate the use of

The style code is as follows:

<?PHPAbstract classshopping{ Public functionBuy () {Echo"Buy"; }     Public functionloan () {Echo"Loan"; }}classLesliebuyextendsshopping{}//$test 1=new shopping;//Direct syntax error$leslieSie=Newlesliebuy;$leslieSie->loan ();//Print out loan?>

PHP in public,private,protected,abstract and other keyword usage detailed

Related Article

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: 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.