Ascii_Demo

Time:2020-9-25

Static keyword

  • initialization:

1. Variables

Initialization can be initialized to basic constants such as numbers, characters, arrays, etc

Ex: static $a = 1, static $a = array(), etc

However, it cannot be initialized to variables, objects, expressions, function return values, etc

Ex: $B = 1; static $a = $B, static $a = new obj(), etc

  • Assignment:

1. Variable: static can be assigned any value, which is consistent with general variable

  • Call:
  1. Modifying member variables

Class name: variable name, self (parent): variable name, static:: variable name

Note: you cannot use $this – > variable name or $obj – > variable name

  1. Modification method

Class name: method name, self (parent): method name, static:: method name, $this – > method name or $obj – > method name

  • Scope:
  1. Modification method

You cannot use $this to call other non static methods or other member variables within a static method.

But you can create an object and call its method or member variable.

ex: $a = new ObjA(); $a -> a;

  • Inheritance:

Static variables of the parent class cannot be modified to other non static variables in the subclass

ex: class A {public static $a = 1} class A extends B{ public $a = 1}

But it can be reinitialized

ex class A {public static $a = 1} class A extends B{ public static $a = 0}