PHP object-oriented-static

Source: Internet
Author: User
PHP object-oriented ---- static as we all know, the goal of OOP is to write code, copy, copy, and inherit, and then use each job reasonably, it also facilitates code management. Find the link where an error occurs. But sometimes the class we write will eventually find that it is only used once and does not generate multiple PHP object-oriented ---- static
As we all know, the goal of OOP is to write code once, copy, copy, inherit, inherit, and then inherit to make reasonable use of every job, and also facilitate code management. Find the link where an error occurs. However, sometimes the class we write finds that it is only used once and does not generate multiple instances. At this time, the new method will not only affect the efficiency, but the code is not concise enough.
Therefore, php, which is very emotional, provides us with a convenient and efficient method of static. with it, it will no longer be a problem.

Class hw {public static function hi () {return 'Hello World !!! ';}} Echo hw: hi (); view plaincopy to clipboardprint? Class hw {public function hi () {return 'Hello World !!! ';}} Echo hw: hi (); as shown in the preceding example, after static and static attributes are used, the method can be directly called from the outside, however, static is recommended for efficiency and other factors. Static class internal call Method class foo {private static function c () {return 'ABCD';} public static function a () {echo self: c () ;}} foo:: a (); when the static keyword is used to limit the method, self: must be used for reference within the class. the above example has been quite clear. Static attribute view plaincopy to clipboardprint? Class foo {public static $ a; public static function a () {self: $ a = 'abc' ;}} foo: a (); echo foo :: $ a; we can also use the static keyword to limit the variable. at this time, the variable will not keep the value of the last negative value. Static inheritance and use view plaincopy to clipboardprint? Class foo {public static $ a; public static function a () {return 'ABCD';} class oo extends foo {public static function () {echo '000000';} oo: a (); the inheritance of static is the same as that of an ordinary class, and there is no big difference. View plaincopy to clipboardprint? Class foo {public static $ a; public static function a () {return 'ABCD';} class oo extends foo {public static function aa () {echo parent :: a () ;}} oo: aa (); it is only used when there is a write difference, must be parent: Method to reference the method of the parent class, and cannot directly self :: in the following example, no value is displayed: view plaincopy to clipboardprint? Class foo {public static $ a; public static function a () {return 'ABCD';} class oo extends foo {public static function aa () {echo self :: a () ;}} oo: a (); another simple example is view plaincopy to clipboardprint?
 ?
     
 

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.