I am a little confused. what should I use static in the class?

Source: Internet
Author: User
I am a little confused. under what conditions should I use static in the class? & lt ;? Php & nbsp; class & nbsp; cls {& nbsp; & nbsp; & nbsp it's a bit confusing when static is used in the class.

    
    class cls{
        
        public static function print_str(){
            
            echo 'OK';
            
        }
        
        
    }
    cls::print_str();

?>




The static method is a static method, which can be directly used without an instance class.

Under what circumstances should static be used to define class methods? is it based on convenient calls?


If there is only one method in the class, it is defined as static or non-static; there is no difference in efficiency.

It should be a relatively simple question, but I have to ask a question if I have a good foundation. Thank you. Class static
------ Solution --------------------
The static method is a function with limited scope, that is, the host name must be used.
I don't see any advantages if I use it only as a function.

If it is used for Object connection in Singleton mode, it is more convenient.
Object transfer is not required
------ Solution --------------------
Reference:
The static method is a function with limited scope, that is, the host name must be used.
I don't see any advantages if I use it only as a function.

If it is used for Object connection in Singleton mode, it is more convenient.
Object transfer is not required


Compared with non-static methods, it does not need to instantiate the entire class. It consumes less performance. Compared with global functions, it can use class features. Inheritance, encapsulation, and other features.

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.