In php, the finalstatic $ this keyword is mainly used in a class or object. next I will record my notes on learning finalstatic $ this. if you need to know about it, refer to the parameter
In php, the final static $ this keyword is mainly used in classes or objects. next I will record my final static $ this Notes. For more information, see.
Final keywords
The php final keyword is used before a function or class name, indicating that the function cannot be rewritten or the class cannot be inherited.
1. The final method cannot be rewritten.
If you do not want a method in the class to be overwritten by the quilt class, you only need to add the keyword final before this method, that is, set the final method.
The instance code is as follows:
- Class ex1 {
- Final function fn1 (){
- Return "php ";
- }
- }
- Class ex2 extends ex1 {
- Function fn1 (){
- Return "html ";
- }
- }
- $ P = new ex2 ();
- Echo $ p-> fn1 ();
- ?>
Methods with the final keyword cannot be overwritten, and an error occurs during running.
2. the final class cannot be inherited.
The declared final class cannot be inherited; otherwise, an error occurs.
The instance code is as follows:
- Final class ex1 {
- Private $ name;
- }
- Class ex2 extends ex1 {
- Private $ age;
- }
- ?>
Static keywords
The php static keyword can be used not only to declare a static variable, but also to declare static attributes or methods in the class to be "class attributes" or "class methods ".
1. after static attributes and methods are declared, the $ this keyword cannot be used for reference in the class. the following two methods can be used for reference:
In the class: self: static member name
Out-of-class: class name: static member name
2. when accessing this class, you can directly use the class name: static member name format without instantiation.
Static attribute instance:
The instance code is as follows:
- Class user {
- Private static $ count = 10;
- Private static $ sum = 1;
- Public function _ construct (){
- For ($ I = 1; $ I <11; $ I ++ ){
- Self: $ sum = self: $ sum * self: $ count;/* call static variables */
- Self: $ count --;
- }
- }
- Public function getCount (){
- Return self: $ sum;
- }
- }
- $ User1 = new user ();
- Echo $ user1-> getCount ();
- ?>
Static method instance:
The instance code is as follows:
- Class Math {
- Public static function sum ($ num1, $ num2 ){
- Return $ num1 + $ num2;
- }
- Public static function product ($ num1, $ num2 ){
- Return $ num1 * $ num2;
- }
- }
- $ A = 88;
- $ B = 100;
- Echo "the sum of two numbers :";
- Echo"
";
- Echo Math: sum ($ a, $ B );
- Echo"
";
- $ A = 66;
- $ B = 88;
- Echo "the product of two numbers is :";
- Echo"
";
- Echo Math: product ($ a, $ B );
- ?>
$ This keyword
To solve the naming conflicts and uncertainty between variables and attributes in php classes and objects, the "$ this" keyword is introduced to call the current object.
You must use the "$ this->" keyword to call the attributes and methods of the current object in the class. $ this indicates the new object created by the constructor in the constructor; the local variables in the method do not belong to the object and do not use the value of $ this.
With the $ this keyword, we can call object attributes or methods in the class.
1. call variables
The instance code is as follows:
- Class user {
- Private $ n;
- Function _ construct (){
- $ Name = "Mike ";
- Echo $ this-> n = $ name;
- }
- }
- $ P = new user ();
- ?>
2. call method
The instance code is as follows:
- Class cal {
- Public function sum ($ a, $ B ){
- Return $ a + $ B;
- }
- Public function prt ($ a, $ c ){
- Return $ a * $ c;
- }
- Public function result ($ a, $ B, $ c ){
- $ A = $ this-> sum ($ a, $ B );
- Return $ this-> prt ($ a, $ c );
- }
- }
- $ C = new cal ();
- Echo "(2 + 3) * 10 =". $ c-> result ('2', '3', '10 ');
- ?>