Example of static method usage in PHP class

Source: Internet
Author: User
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:

  1. Class
  2. {
  3. Static function fun ()
  4. {
  5. // Do somathing
  6. }

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:

  1. Class MyClass
  2. {
  3. Public $ num = 5;
  4. Function _ construct ()
  5. {
  6. $ This-> num = 10;
  7. }
  8. Function fun_1 ()
  9. {
  10. Echo "I am a public method named fun_1.n ";
  11. Echo "The num of object is {$ this-> num}. n ";
  12. }
  13. Static function fun_2 ()
  14. {
  15. Echo "I am a static method named fun_2.n ";
  16. }
  17. Function fun_3 ($ n)
  18. {
  19. Echo "The arg is {$ n} n ";
  20. }
  21. }
  22. $ M = new MyClass;
  23. $ M-> fun_1 ();
  24. $ M-> fun_2 ();
  25. $ M-> fun_3 ('test ');
  26. MyClass: fun_1 ();
  27. MyClass: fun_2 ();
  28. MyClass: fun_3 ('test ');
  29. /*
  30. Output result:
  31. Lch @ localhost: php $ php class_method.php
  32. I am a public method named fun_1.
  33. The num of object is 10.
  34. I am a static method named fun_2.
  35. The arg is test
  36. I am a public method named fun_1.
  37. PHP Fatal error: Using $ this when not in object context in/Users/lch/program/php/class_method.php on line 14
  38. */

Let's look at another instance. the code is as follows:

  1. Class user {
  2. Public static $ username; // declare a static attribute
  3. Public $ password; // declare a non-static attribute
  4. Function _ construct ($ pwd ){
  5. Echo 'username: ', self: $ Username; // output static attributes
  6. Self: $ username = 'admin'; // assign a value to the static attribute
  7. $ This-> password = $ pwd; // value for non-static attributes
  8. }
  9. Public function show () {// output class attributes
  10. Echo'
  11. Username: ', self: $ username;
  12. Echo'
  13. Password: ', $ this-> password;
  14. }
  15. Public static function sshow (){
  16. Echo'
  17. Username: ', self: $ username;
  18. Echo'
  19. Password: ', $ this-> password;
  20. }
  21. }
  22. User: $ username = 'root'; // assign a value to the static attribute of the user class.
  23. $ ObjUser = new user ('000000'); // instantiate the user class
  24. $ ObjUser-> sshow ();
  25. Unset ($ objUser );
  26. Echo'
  27. Username: ', user: $ username;
  28. /*
  29. * The output result is:
  30. * Username: root
  31. * Username: admin
  32. * Password: 123456
  33. * Usern
  34. Ame: admin
  35. **/

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:

  1. Class Log
  2. {
  3. Public $ root = DIRECTORY_SEPARATOR;
  4. Public $ logPath = '/data/app/www/test-realtime.monitoring.c.kunlun.com/log ';
  5. Public $ defadir dir = 'default ';
  6. Public function writeLog ($ logName, $ logType, $ data, $ newDir = FALSE)
  7. {
  8. $ FileName = '';
  9. If (! File_exists ($ this-> logPath ))
  10. {
  11. Mkdir ($ this-> logPath, 0777 );
  12. }
  13. If ($ newDir! = FALSE)
  14. {
  15. @ Mkdir ($ this-> logPath. $ this-> root. $ newDir, 0777 );
  16. $ FileName = $ this-> logPath. $ this-> root. $ newDir. $ this-> root. date ('Y-m-D', time ()). '_'. $ logName. '_'. $ logType. '. log ';
  17. }
  18. Else
  19. {
  20. @ Mkdir ($ this-> logPath. $ this-> root. $ this-> defaultDir, 0777 );
  21. $ FileName = $ this-> logPath. $ this-> root. $ this-> defaultDir. $ this-> root. date ('Y-m-D', time ()). '_'. $ logName. '_'. $ logType. '. log ';
  22. }
  23. File_put_contents ($ fileName, date ('Y-m-d H: I: s'). ''. $ data." n ", FILE_APPEND );
  24. }
  25. }

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.

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.