PHP static tips

Source: Internet
Author: User

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 ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.