PHP static variable

Source: Internet
Author: User

1 static variables within a function
Only works inside the function, and after each call, the value of the static variable is changed on the basis of the last call. When defined, if the initial value is given, then the statement executes only once
For example: No matter how many times the GetData () function is called static $c 2 = 0; This statement will only be executed once
function GetData () {
static $c 2 = 0;//initialization statement
$c 2 = $c 2 + 1;
echo "$c 2\n";
}
GetData ();//result 1
GetData ();//Result 2

2 Static class member variables
1. Static member variables of a class belong to this class only, but all instances of the class share this static member variable
2. Static member variables can be accessed without instantiation and are accessed faster
For example:
Class C1 {
Use Counter;
static $c 2=1;
Public Function Inc () {
static $c;
$c = $c + 1;
echo "$c \ n";
}
}
$o = new C1 ();
$o->inc (); Echo 1
$p = new C1 ();
$p->inc (); Echo 2
Echo
‘;
$p:: $c 2=5;
echo C1:: $c 2;//echo 5 static member variables can be accessed without instantiation and are accessed faster
Echo
‘;
echo $p:: $c 2;//echo 5
Echo
‘;
echo $o:: $c 2;//echo 5

    1. Static Class method
      A static method of a class can only access static member variables and cannot access non-static member variables (error if any)
      Class C3 {
      Use Counter;
      public static $c 3=1;
      Public $c 5 = 5;
      public static function Inc1 () {
      echo C3:: $c 3;//
      }
      public static function Inc2 () {
      Echo $this->c5; Error $this not allowed in static methods
      }
      }
      C3::INC1 ();//echo 1
      C3::INC2 (); Fatal error:uncaught error:using $this When not in object context

    2. Static variables for Trait
      Static variables of the trait are not affected by different classes when they are used
      For example:
      Trait Counter {
      Public Function Inc () {
      static $c = 0;
      $c = $c + 1;
      echo "$c \ n";
      }
      }

      Class C1 {
      Use Counter;
      }

      Class C2 {
      Use Counter;
      }

      $o = new C1 ();
      $o->inc (); Echo 1
      $o->inc (); Echo 2
      $b = new C1 ();
      $b->inc (); Echo 3
      $b->inc (); Echo 4
      $p = new C2 ();
      $p->inc (); Echo 1
      $p->inc (); Echo 2

PHP 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.