PHP Static keyword utility methods

  • 2020-03-31 20:47:53
  • OfStack

To be compatible with PHP4, properties and methods default to public if visibility is not specified.
Since static methods do not need to be called through an object, the pseudo-variable $this is not available in static methods.
Static properties can also be passed by objects -> Operator to access.
Calling a non-static method in :: causes an E_STRICT level error.
Like all other PHP static variables, static properties can only be initialized to a character value or a constant, not an expression. So you can initialize static properties as integers or arrays, but you can't point to another variable or function return value, and you can't point to an object.
After PHP5.3.0, we can call the class dynamically with a variable. But the value of this variable cannot be the keyword self, parent or static.
 
<?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 . "n"; 
$foo = new Foo(); 
print $foo->staticValue() . "n"; 
print $foo->my_static . "n"; // Undefined "Property" my_static 
print $foo::$my_static . "n"; 
$classname = 'Foo'; 
print $classname::$my_static . "n"; //PHP 5.3.0 can be called dynamically
print Bar::$my_static . "n"; 
$bar = new Bar(); 
print $bar->fooStatic() . "n"; 
?> 

PHP USES the Static keyword to define Static properties and methods.

Example 1: reference methods for static properties
 
<?php 
 
class person{ 
static$name="ajax123";//Static declaration static properties
static$age=25;//Static declaration static properties
static$address=" Beijing ";//Static declaration static properties
function song(){ 
echo "My name is : ".self::$name."<br>";//Inside the class: by accessing static properties through the self class
echo "I am ".self::$age."<br>";//Inside the class: by accessing static properties through the self class
echo "I live in ".self::$address."<br>";//Inside the class: access static properties through the self class
} 
} 
echoperson::$name."<br>";//Outside of the class: access static properties by the class name person
echoperson::$age."<br>";//Outside of the class: access static properties by the class name person
echoperson::$address."<br>";//Outside of the class: access static properties by the class name person
?> 


Example 2: reference methods for static methods

 
<?php 
 
class person{ 
static$name="ajax123";//Static declaration static properties
static$age=25;//Static declaration static properties
static$address=" Beijing ";//Static declaration static properties
staticfunction song(){ //Declare the static method song
echo "My name is : ".self::$name."<br>";//Inside the class: by accessing static properties through the self class
echo "I am ".self::$age."<br>";//Inside the class: by accessing static properties through the self class
echo "I live in ".self::$address."<br>";//Inside the class: access static properties through the self class
} 
} 
person::song()."<br>";//External to class: access static methods by the class name person
?> 

Related articles: