When some tickets are worth it, we often write this way to call a method.
$ A = A: getInstance ();
$ A-> setCount (10 );
$ A-> showCount ();
We can change setCount to a static method.
Put A: getInstance () in this method
We can write it like this.
A: setCount (10 );
The three lines of code are changed to one line,
Class implementation:
Copy codeThe Code is as follows: <?
Class
{
Private $ _ count = "";
Static private $ _ instance = null;
Private function _ construct ()
{
}
Public function showCount ()
{
Echo $ this-> _ count;
}
Static public function setCount ($ num)
{
$ Obj = self: getInstance ();
$ Obj-> _ setCount ($ num );
Return $ obj;
}
Private function _ setCount ($ num)
{
$ This-> _ count + = $ num;
}
Static private function getInstance ()
{
If (is_null (self: $ _ instance ))
Self: $ _ instance = new self ();
Return self: $ _ instance;
}
}
A: setCount (5 );
A: setCount (10)-> showCount ();
?>