Talk about the usage of public,private,protected,abstract and other keywords in PHP

Source: Internet
Author: User
The following small series for you to share a detail in PHP public,private,protected,abstract and other key words of use, with a good reference value, I hope to be helpful to everyone. Let's take a look at it with a little knitting.

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*/class myclass{Public $public = "public"; protected $protected = ' protected '; private $priv ate = ' Private '; Public Function Printhello () {echo $this->public; echo $this->protected; echo $this->private;} protected Functi On Pro_test () {var_dump (1);}} $obj = new MyClass (); Echo $obj->public; This line can be executed normally//echo $obj->protected; This guild produces a fatal error.//echo $obj->private; This line will 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:

<?phpfunction 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.

<?phpfunction 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 ();//3 </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 ++;function test1 () {Static $age = +; $age + +; echo $age. " </br> ";} function Test2 () {Static $age = +, $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

Class BaseClass {Public $public = ' public ', function test () {echo "baseclass::test () called\n";} Final Public function Moretesting () {echo "baseclass::moretesting () called\n";}} Class ChildClass extends BaseClass {public Function moretesting () {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:

<?phpinterface testa{function Funca ();} Interface testb{function FUNCB ();} Interface TESTC extends TestA {function funcc ();}  Class Run implements TESTC, Testb {public Function Funca () {//Todo:implement Funca () method.} Public Function FUNCB () {//Todo:implement FUNCB () method.} Public Function FUNCC () {//Todo:implement FUNCC () method.}}? >

Abstract has the same effect as interface, but all the methods in interface must be implemented, but in the class of the abstract modification, there can be one or more abstract modifiers, so we can understand that 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 class shopping{public function Buy () {echo "buy",} public Function loan () {echo "Loan";}} Class Lesliebuy extends shopping{}//$test 1=new shopping;//Direct syntax error $lesliesie = new Lesliebuy; $leslieSie->loan ();// Print out loan?>

The above details in PHP public,private,protected,abstract such as the use of key words are small parts to share all the content, I hope to give you a reference, but also hope that we support PHP Chinese network.

Articles you may be interested in:

PHP lets you make a new array instance with the same values in the array

Troubleshooting PHP string Length inconsistencies

The command line executes the $ARGV and $ARGC configuration methods in the PHP script _php instance

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.