PHP static variable _ PHP Tutorial

Source: Internet
Author: User
Talk about static variables in PHP. Static variables only exist in the function scope. that is to say, static variables only exist in the stack. Generally, variables in a function are released after the function ends, such as local variables, but static variables only exist in the function scope. that is to say, static variables only survive in the stack. Generally, variables in a function are released after the function ends, such as local variables, but static variables do not. That is to say, when you call this function again, the value of this variable will be retained.

As long as the keyword static is added before the variable, the variable becomes a static variable.

    
 ";}// First execution, $ nm = 2 test (); // first execution, $ nm = 4 test (); // first execution, $ nm = 8 test ();?>

Program running result:

248

After the test () function is executed, the values of the variable $ nm are saved.

Static attributes are often used in class, such as static members and static methods.

Program List: static member of the class

The static variable $ nm belongs to bkjia but not to an instance of the class. This variable is valid for all instances.

: It is a scope-limited operator. here, the self scope is used, instead of $ this scope. $ this scope only indicates the current instance of the class, and self: indicates the class itself.

    
 ';}}$nmInstance1 = new bkjia();$nmInstance1 -> nmMethod();$nmInstance2 = new bkjia();$nmInstance2 -> nmMethod();?> 

Program running result:

35
Program List: Static attribute
    
 "; // Call the class method $ bkjia = new NowaMagic (); print $ bkjia-> n1_hod ()."
"; Print Article: $ nm ."
"; $ NmArticle = new Article (); print $ nmArticle-> n1_hod ()."
";?>

Program running result:

www.bkjia.comwww.bkjia.comwww.bkjia.comwww.bkjia.com
Program List: a simple static constructor

PHP does not have a static constructor. you may need to initialize a static class. there is a very simple method that directly calls the Demonstration () method of the class after the class definition.

    
 

Program running result:

This is the result of demonstration()

Bytes. The variable in a common function will be released after the function ends, for example, a local variable, but static variable...

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.