The static keyword in PHP affects variables and functions

Source: Internet
Author: User
Tags modifier

 
1 the global variable (external variable) before the description and then the static to form a global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the entire source program, and 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, that is, it is only valid within the source file that defines the variable, and it 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 only be common to functions within that source file, so it is possible to avoid causing errors in other source files.

2 from the above analysis can be seen, the local variable to the static variable is changed its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its 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 internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, you should indicate in a header file that the source file to use these functions contains this 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


<?php
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 invoked dynamically after

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

Example #2 static Method code example

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

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

For the use of the static keyword in a class, the PHP manual provides the following conventions:


1. Declaring a class member or method static, you can access it directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.

2, because the static method does not need to pass the object to call, therefore the pseudo variable $this in the static method is not available.
3, static properties can not be accessed by the object through the-> operator.
4, with:: The way to call a non-static method can cause a e_strict level error.
Now let's look at the 4th agreement.
Operating environment: (WIN32) php/5.3.3

The code is as follows Copy Code

Class foo{

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


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

}

}

echo Foo:: $my _static. "<br >";

echo Foo::run ()///non-static method called by ClassName:: Method Name
Echo Foo::callrun ();

The static keyword of the interview question


Title Code: Write out the result of the operation of the following code ()

The code is as follows Copy Code

<?php

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 to add the calculation: The result is 400, the answer is wrong. Finally looked at the next, knocking on one side of the code, run to know is 500. Two $i+= $n were printed, $i before the calculation, once 100, once 200, knowing that the problem was static ghost. Then baidu a static keyword, before it dawned.

Static keyword action:

Static variables in PHP are more widely used, not only can we add the static modifier to the class, method, or variable, we can even add the static keyword to the function's internal variables. A variable with the static modifier added even if the function completes the value is not lost, that is, the variable still remembers the original value the next time the function is called. For example,

The code is as follows Copy Code

<?php

function test ()

03 {

static $var 1 = 1;

$var 1 +=2;

1 echo $var. ' ' ;

07}

08

Test ();

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:
Static global variable is only first made once, preventing it from being referenced in other file cells;
What is the difference between a static local variable and a normal local variable:
A static local variable is initialized only once, the next time based on the last result value, and the
static function differs from a normal function:
The static function is only one copy in memory, The normal function maintains a copy of each invocation

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.