The static keyword in PHP affects variables and functions _php tutorial

Source: Internet
Author: User
If static is a static variable in PHP, and he can define a function, and the variable is a global static variable, then how do we add static to the function or variable that will affect the functions and variables, let's look at it together.

 
1) The description of the global variable (the external variable) is then prefixed with static, which constitutes the global variable. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the entire source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.

2) from the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.

3) The static function differs from the normal function scope only in this file. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to which the function is to be used is to contain the header file


After PHP5.3.0, we can use a variable to invoke the class dynamically. However, the value of the variable cannot be the keyword self, parent, or static.

Example #1 static Member code example

The code is as follows Copy Code


Class Foo
{
public static $my _static = ' foo ';

Public Function Staticvalue () {
Return self:: $my _static;
}
}

Class Bar extends Foo
{
Public Function foostatic () {
Return Parent:: $my _static;
}
}


Print Foo:: $my _static. " ";

$foo = new Foo ();
Print $foo->staticvalue (). " ";
Print $foo->my_static. " "; Undefined "Property" My_static

Print $foo:: $my _static. " ";
$classname = ' Foo ';
Print $classname:: $my _static. " "; PHP 5.3.0 can be dynamically called after

Print Bar:: $my _static. " ";
$bar = new Bar ();
Print $bar->foostatic (). " ";
?>

Example #2 static Method code example

Class Foo {
public static function Astaticmethod () {
// ...
}
}

Foo::astaticmethod ();
$classname = ' Foo ';
$classname:: Astaticmethod (); As of PHP 5.3.0
?>

With regard to the use of the static keyword in the class, the PHP manual gives the following conventions:


1, declaring a class member or method is static, you can not instantiate the class and directly access. Static members cannot be accessed through an object (except static methods).

2, because the static method does not need to be called through the object, so the pseudo-variable $this in the static method is not available.
3. Static properties cannot be accessed by the object by operator.
4, using:: The way to call a non-static method will cause a e_strict level error.
Now take a look at the 4th Convention.
Operating environment: (WIN32) php/5.3.3

The code is as follows Copy Code

Class foo{

public static $my _static = ' foo ';//declare a static member


Public Function Staticvalue () {//static method
Return self:: $my _static;//
}
Public Function Run () {//non-static method
Return "ABC
";
}
Public Function Callrun () {
return Self::run ();//Use Self:: method to invoke a non-static method

}

}

echo Foo:: $my _static. "
";

Echo Foo::run ();//Use ClassName:: Method name to invoke non-static method
Echo Foo::callrun ();

The static keyword of the interview question


Title Code: Write the results of the following code ()

The code is as follows Copy Code

function Dewen () {
$k =add_number (100);
$k +=add_number (100);
printf ("%d", $k);
return 0;
}

function Add_number ($n) {
static $i = 100;
$i + = $n;
return $i;
}

The first thought is simply self-calculation: Think the result is 400, the result is wrong answer. Finally carefully looked down, knocked on one side of the code, run to know is 500. Printed two times $i+= $n; calculate the previous $i, once is 100, once is 200; know that the problem is static in Ghost. Then baidu a bit static keyword, just suddenly dawned.

Static keyword effect:

The use of static variables in PHP is wider, and we can add the static modifier to a class, method, or variable before we even add the static keyword to the function's internal variables. A variable with the static modifier added is not lost even after the function has finished executing, that is, the variable remembers the original value the next time the function is called. Such as:

The code is as follows Copy Code

01

The function test ()

03 {

static $var 1 = 1;

$var 1 +=2;

echo $var 1. ' ' ;

07}

08

The test ();

Ten test ();

one Test ();

?>
The results of the operation are as follows:

3 5 7

Sum up:

What is the difference between a static global variable and a normal global variable:
The static global variable is only initialized once, preventing it from being referenced in other file units;
What is the difference between a static local variable and a normal local variable:
The static local variable is initialized only once, the next time based on the last result value;
What is the difference between a static function and a normal function:
The static function has only one copy in memory, and the normal function maintains one copy of each call.

http://www.bkjia.com/PHPjc/632692.html www.bkjia.com true http://www.bkjia.com/PHPjc/632692.html techarticle in PHP is static variable, he can define a function, the variable is a global static variable, then we add static before the function or variable to the function and the variable will be what kind of ...

  • Related Article

    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.