static properties and methods

Why are there static properties

Just for convenience, you can use the methods inside without instantiating a class


code practice

class math{

        static public function add($a,$b){

        return $a+$b;

    }

}

$sum = new math();

$sum -> add(2,3);

The return result of this writing is 5

But you can also do not need to instantiate this class, you need to add static before the method

Use the trick math::add(2,3) without instantiating this class


class constant call

define('PI',3.1415926);

class math{

    public function addc(){

        echo PI; //The call is a global constant

    }

}

$bb = new math();

$bb->addc();

So the problem is that if I need to call the constants in the class, I need to write like this

class math{

    const PI = 3.1415926;

    public function addc(){

        echo math::PI;

    }

}

$bb = new math();

$bb -> addc();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853220&siteId=291194637