Use of static keywords for Object-Oriented Knowledge points

Source: Internet
Author: User

Use of static keywords for Object-Oriented Knowledge points
/* The attributes and methods defined by static are called static members and static methods. Static attributes and Methods belong to Classes and are shared among objects. */
/* For example, you can define a static variable to count the number of objects instantiated by the class */
Class test {
Static $ count;
Function _ construct () // defines a constructor.
{
Self: $ count ++;
}
Static function getCount () {// defines a static method and returns the value of the static variable $ count.
Return self: $ count;
}
}
Test: $ count = 0; // The initial static variable $ count is 0.
$ Test_01 = new test ();
$ Test_02 = new test ();
$ Test_03 = new test ();
$ Sum = test: getCount ();
Echo $ sum;
// Result 3
/*
You can use
Class Name: static member; // access static member
Class Name: static method; // access static method
Static Methods in the class can be passed through
Self: static member; // access static member
Self: static method; // access static method
Note: Only static members can be accessed in static methods.
*/
?>

 

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.