你不应该,它很少有用.静态的常见用法是工厂方法和singleton :: instance()
厂:
class Point{
private $x;
private $y;
public function __construct($x, $y){
...
}
static function fromArray($arr){
return new Point($arr["x"], $arr["y"]);
}
}
Run Code Online (Sandbox Code Playgroud)
单:
class DB{
private $inst;
private function __construct(){
...
}
static function instance(){
if ($this->inst)
return $this->inst;
return $this->inst = new DB();
}
}
Run Code Online (Sandbox Code Playgroud)