Use the $ this, static, final, const, and self keywords in the php class

Source: Internet
Author: User
The keywords $ this, static, final, const, and self in the php class are often used in projects. this article will share this, static, final, const, self: If you are interested in using these keywords, let's take a look at the $ this, static, final, const, how to use the keywords self.

  $ This

$ This indicates the current instance. when the internal method access of the class is not declared as a const or static attribute, $ this-> value = 'phpernote'; is used. Common usage:

$ This-> attribute

$ This-> method

Example:

<? Phpclass MyClass {private $ name; public function _ construct ($ name) {$ this-> name = $ name;} public function getname () {return $ this-> name;} public function printName () {echo $ this-> getname () ;}$ myclass = new MyClass ("I Like www.bitsCN.com "); $ myclass-> printName (); // output: I Like www.bitsCN.com?>

There are three methods to call the attributes and methods of the current class in the class: self, parent, and $ this. The difference between the three keywords is: self is used to direct to the current class; parent is used to point to the parent class of the current class. you can use this keyword to call the attributes and methods of the parent class. $ this is used to call its own attributes and methods in the class body.

  Static

The keyword can be self (used when a static member is called within the class) the class name of the static member (used when a static member inside the class is called outside the class)
Declare a static variable as follows:

static $val='';

Variables only exist in the function scope. after the function is executed, the value of the variable will not be lost and will only be initialized once. the expression cannot be used to initialize static variables, you do not need to replace global variables because they are easily accessed by all functions and thus are not suitable for maintenance.
Using static in a class has two main purposes: defining static members and defining static methods. Static members only keep the value of one variable. this value is valid for all instances, as shown below:

<? Phpclass MyObject {public static $ myStaticVar = 0; function myMethod () {self ::$ myStaticVar + = 2; echo self :$ myStaticVar ;}} $ instance1 = new MyObject (); $ instance1-> myMethod (); $ instance2 = new MyObject (); $ instance2-> myMethod (); // The result is printed 2 and 4 respectively.

The code is as follows:

<? Phpclass Book {static $ num = 0; public function showMe () {echo "". self: $ num. "guest"; self: $ num ++; }}$ book1 = new Book (); $ book1-> showMe (); echo"
"; $ Book2 = new Book (); $ book2-> showMe (); echo"
"; Echo" you are a drop ". Book: $ num." guest ";?>

The result will be:

You are a visitor of 0
You are a visitor.
You are two visitors

In addition, it should be noted that if the class method is static, the Accessed attribute must also be static.

  Final

PHP final keywords can modify classes and methods in classes, but they have similar functions, that is, if you use final keywords to modify them, the modified class or method cannot be extended or inherited. You can only honestly reference it. If you use final before the class, this indicates that this class cannot use inheritance. if you use the PHP final keyword before the method, this indicates that this method cannot be overwritten. The truth is that simple. let's take a look at a simple example.

The final class and method cannot be inherited, and the method modified by this keyword cannot be overwritten. The general usage is as follows:

Final class MyClass {// This class will not be allowed to be inherited from final function fun1 () {...} // this method will not be allowed to be overwritten}

Example

 < ?PHP    final class BaseClass {    public function test() {    echo "BaseClass::test() calledn";    }    final public function moreTesting() {    echo "BaseClass::moreTesting() calledn";    }    }    class ChildClass extends BaseClass {    public function moreTesting() {    echo "ChildClass::moreTesting() calledn";    }    }    // Results in Fatal error: Cannot override final method BaseClass::moreTesting()    ?> 

  Const

When you access the attributes declared as const and static by using internal methods of the class, you must use self ::$ name to call them. Example:

<? Phpclass clss_a {private static $ name = "static class_a"; const PI = 3.14; public $ value; public static function getName () {return self: $ name ;} // This statement is incorrect. the static method cannot access the non-static attribute public static function getName2 () {return self: $ value;} public function getPI () {return self :: PI ;}}

Note that the declarative format of the const attribute is const PI = 3.14, rather than const $ PI = 3.14.

 Self

Self indicates the class itself, pointing to the current class. It is usually used for static members, methods, and constants of the category class.

The above content briefly introduces the usage of the $ this, static, final, const, and self keywords in the php class.

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.