PHP Method for referencing local static variables of operations and external operation functions, static variables

Source: Internet
Author: User

PHP Method for referencing local static variables of operations and external operation functions, static variables

Static variables in external operation functions or member methods by reference

The following is a simple example to illustrate three references:

1. type conversion in the function after parameter reference is also an address operation

2. If the parameter is referenced and passed to another function, you must add a reference character to maintain the address operation.

3. The reference operator must be added when the function is declared and called.


This example uses Object method operations and is also applicable to functions.

<? Phpclass A {public function & test1 (& $ a) {static $ I = 0; var_dump ($ I); $ a = (array) $; $ a ['domain '] = 'HTTP: // blog.csdn.net/zhouzme'{$this-> test2 ($ a); $ I ++; var_dump ($ a); var_dump ($ I ); return $ I;} protected function test2 (& $ a) {$ a ['name'] = 'snail ';}}$ obj = new (); $ a = ''; $ c = & $ obj-> test1 ($ a); $ c ++; var_dump ($ ); $ obj-> test1 ($ a); var_dump ($ );


Output result








References to local static variables first

Look at this question: e is declared outside the function body. It is not a local variable and it is global, in this source file, all functions following the static int e statement can be used, and the extern keyword is not required.

If you correctly asked whether a static local variable is available in other functions? The answer is no.
Local variables are only available in this function, whether static or not

PHP has no Global static variables, such as class {public static int INY = 0;} Like java, which can be called on any jsp page

There are global variables, but if you want to call files that require include or require containing global variables on different pages.
Global keywords are required to call global variables in a function or class.
Example:
1. php
<? Php
$ A = "test ";
?>
2. php
<? Php
Include ("1.php ");
// Or require ("1.php ");
Echo $;
// Output test
?>

Function call
<? Php
$ A = "test ";
// You can also use require/include from other files
/* It can also be a class */
Function (){
Global $ a; // introduce global variables
Return $;
// You can also remove global and directly return $ GLOBAL ['a'];
}
Echo a (); // output test
?>

If you need a real global variable, you can use session or cookie to store it.
References: Years of programming experience

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.