php class knowledge ---- class constants, static variables

  • Class constants
<?php
class myuser
{
    const Age = 34 is ; // defined constants without the $ symbol, no access modifier A constant, unmodified symbols A constant 
    public function Monolog ()
    {
        echo " this year " .self :: Age, " years old, wanted to programmers, as well as play it " . " \ the n- " ;
        echo " ? What do you have " . $ the this :: Age. " , and all? " . " \ the n- " ;
    }
}

$dy = new myuser();
$ this -> monolog ();
echo " This year I " .. $ dy :: Age " years old " . " \ the n- " ;
echo " This year I " .myuser :: Age. " years old " . " \ the n- " ;
 ?>

Output:

34 years old this year, I want to programmers, as well as play it
what? You have 34, and all?
This year I am 34 years old and
this year I'm 34 years old.

 

  • --- static variables static variables and methods belong to the class, so can not appear $ this-> static method name, but it is possible to access the static method name :: obj way through the object.
<?php
class wenwa
{
    public static $rules = "不准用手机"."\n";
    static public $name = "my name is taliban"."\n";
    public static function self_introduce()
    {
        echo "there is a game of love"."\n";
    }
}
$ match = ' wenwa ' ;
$ root = new $ file;
$saiwa::self_introduce();
echo wenwa::$name;
?>

Output:

there is a game of love
my name is taliban

Guess you like

Origin www.cnblogs.com/saintdingspage/p/10961725.html