This article mainly describes how to use the $ this, static, final, const, and self keywords in the php class. $ This indicates that the current instance is accessed through internal methods of the class.
This article mainly describes how to use the $ this, static, final, const, and self keywords in the php class.
$ 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
The instance code is as follows:
-
- Class 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.111cn.net ");
- $ Myclass-> printName (); // output: I Like www.111cn.net
- ?>
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. the code is as follows:
-
- Class 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 instance code is as follows:
-
- Class Book {
- Static $ num = 0;
- Public function showMe (){
- Echo "you are a drop". 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 cannot be inherited
- Final function fun1 () {...} // this method will not be allowed to be overwritten
- }
The instance code is as follows:
-
- 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 the internal method of the class, you must use the self ::$ name method. the instance code is as follows:
-
- Class 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 non-static attributes.
- 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.