In php, we can directly add a static method before a function or variable. it is similar to a static variable in use and does not need to be instantiated. we can directly use: called, below I
In php, we can directly add a static method before a function or variable. it is similar to a static variable in use and does not need to be instantiated. we can directly use: called, next I will give you a few examples of static methods.
PHP is no exception! The so-called static method (attribute) is the attribute or method marked with the static keyword (for example, the static property public static username ;)
The biggest difference between static and non-static methods is that they have different lifecycles. it is very easy to define static methods by using an instance to describe static methods, the instance code is as follows:
- Class
- {
- Static function fun ()
- {
- // Do somathing
- }
Static method usage
It is similar to static variables and does not need to be instantiated. it can be called directly. the instance code is as follows:
A: fun ()
Comparison of common methods
Because the call to a static method does not need to be instantiated, an error occurs when you reference the attributes or methods of the class in the static method, that is, self and $ this are incorrect, the instance code is as follows:
- Class MyClass
- {
- Public $ num = 5;
- Function _ construct ()
- {
- $ This-> num = 10;
- }
- Function fun_1 ()
- {
- Echo "I am a public method named fun_1.n ";
- Echo "The num of object is {$ this-> num}. n ";
- }
- Static function fun_2 ()
- {
- Echo "I am a static method named fun_2.n ";
- }
- Function fun_3 ($ n)
- {
- Echo "The arg is {$ n} n ";
- }
- }
- $ M = new MyClass;
- $ M-> fun_1 ();
- $ M-> fun_2 ();
- $ M-> fun_3 ('test ');
- MyClass: fun_1 ();
- MyClass: fun_2 ();
- MyClass: fun_3 ('test ');
- /*
- Output result:
- Lch @ localhost: php $ php class_method.php
- I am a public method named fun_1.
- The num of object is 10.
- I am a static method named fun_2.
- The arg is test
- I am a public method named fun_1.
- PHP Fatal error: Using $ this when not in object context in/Users/lch/program/php/class_method.php on line 14
- */
Let's look at another instance. the code is as follows:
- Class user {
- Public static $ username; // declare a static attribute
- Public $ password; // declare a non-static attribute
- Function _ construct ($ pwd ){
- Echo 'username: ', self: $ Username; // output static attributes
- Self: $ username = 'admin'; // assign a value to the static attribute
- $ This-> password = $ pwd; // value for non-static attributes
- }
- Public function show () {// output class attributes
- Echo'
- Username: ', self: $ username;
- Echo'
- Password: ', $ this-> password;
- }
- Public static function sshow (){
- Echo'
- Username: ', self: $ username;
- Echo'
- Password: ', $ this-> password;
- }
- }
- User: $ username = 'root'; // assign a value to the static attribute of the user class.
- $ ObjUser = new user ('000000'); // instantiate the user class
- $ ObjUser-> sshow ();
- Unset ($ objUser );
- Echo'
- Username: ', user: $ username;
- /*
- * The output result is:
- * Username: root
- * Username: admin
- * Password: 123456
- * Usern
- Ame: admin
- **/
From this example, we can see that static attributes work before class instantiation, and can still play a role when the object is destroyed!
Because of this attribute of the static method, you cannot call non-static attributes or methods in the static method.
See
1. in the php class, if the visibility of all attributes and methods is public, the method or attribute of the external class must be called through the object [class instantiation process, the instance code is as follows:
- Class Log
- {
- Public $ root = DIRECTORY_SEPARATOR;
- Public $ logPath = '/data/app/www/test-realtime.monitoring.c.kunlun.com/log ';
- Public $ defadir dir = 'default ';
- Public function writeLog ($ logName, $ logType, $ data, $ newDir = FALSE)
- {
- $ FileName = '';
- If (! File_exists ($ this-> logPath ))
- {
- Mkdir ($ this-> logPath, 0777 );
- }
- If ($ newDir! = FALSE)
- {
- @ Mkdir ($ this-> logPath. $ this-> root. $ newDir, 0777 );
- $ FileName = $ this-> logPath. $ this-> root. $ newDir. $ this-> root. date ('Y-m-D', time ()). '_'. $ logName. '_'. $ logType. '. log ';
- }
- Else
- {
- @ Mkdir ($ this-> logPath. $ this-> root. $ this-> defaultDir, 0777 );
- $ FileName = $ this-> logPath. $ this-> root. $ this-> defaultDir. $ this-> root. date ('Y-m-D', time ()). '_'. $ logName. '_'. $ logType. '. log ';
- }
- File_put_contents ($ fileName, date ('Y-m-d H: I: s'). ''. $ data." n ", FILE_APPEND );
- }
- }
Class instantiation object process: $ logObj = new Log ();
Methods in the callback class: $ logObj-> writeLog ($ param1, $ param2, $ param3, $ param4 );
Attributes in the response class: echo $ logObj-> root;
2. if the attribute in the class is modified by the static keyword, the object cannot be used to access the property modified by the static keyword, however, if the method in the class is modified statically, the object can be accessed through the class name: method name.
3. if the method in the class is modified statically, $ this cannot be used in the method. $ this refers to the instantiated object of the class. because the static method can be called without passing through the object, therefore, the pseudo variable $ this is not available.