Differences between phpstatic static variables and javastatic static variables-php Tutorial

Source: Internet
Author: User
Differences between phpstatic static variables and javastatic static variables Static attributes of java:

<%!static class Count {    private static int count = 0;    public static int getCount() {        count++;                return count;    }}%><%out.print(Count.getCount());out.print("
");out.print(Count.getCount());%>


Output 1 and 2 for the first time

After the refresh, the output is 3 and 4. the refresh will increase progressively.

If you enable tomcat server again after it is restarted, resend 1 and 2


Static attributes of php

 "; Echo Count: getCount ();

Output 1 and 2 for the first time. refresh will still output 1 and 2


Comparison Summary:

1. the static attribute of java disappears along with the exit of the java virtual machine. during the running of the java virtual machine, the static attribute always exists and all users share the static attribute.

2. php's static attribute is only for one request (one php file execution). after the php file is executed, the static attribute disappears immediately and is requested again (execute the php file again ), the static property is re-created. Therefore, the static property of php is not shared and only valid for a single request.


Supplement:

In addition, php also has the concept of static variables. Static attributes are in the class, and static variables are in the method:

Function count () {static $ count = 0; // defines the static variable $ count ++ in the method; return $ count;} echo count (); echo"
"; Echo count (); echo"
"; Echo count ();

Output 1, 2, and 3 for the first time, and output 1, 2, and 3 after Refresh. it indicates that the static variables of php are the same as those of static attributes and are only valid for a single request.


Refer:


Http://www.zhihu.com/question/35472851

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.