How to use PHP static methods and properties _php tutorial

Source: Internet
Author: User
As we all know, the purpose of OOP is to write code once, copy and copy, inherit and inherit the reasonable use of every work, but also convenient code management. Which link is wrong to find which link. But sometimes we write a class that eventually finds it only used once, and does not generate multiple instances. This time again to new, not only affect the efficiency, the code also appears not concise.

So very emotional PHP, provides us with a convenient and efficient way to static with it all is no longer a problem.

Let me tell you about the use of static methods and characteristics.

Hello World:

Class hw{public    static function hi () {       return ' Hello world!!! ';    }} Echo Hw::hi ();  
Class hw{public    function hi () {       return ' Hello world!!! ';    }} Echo Hw::hi ();  

As you can see from the example above, using static and not using the static property can be used directly: The method is called directly from the outside, but it is recommended to use static for efficiency and other reasons.

Static class Internal Call method

Class foo{private static function C () {  return ' ABCD ';} public static function A () {  echo self::c ();}} Foo::a ();  

The use of the static keyword to restrict a method is that you must use self:: Within this class, the above example is very clear.

static property

Class Foo{public static $a;p ublic static function A () {self  :: $a = ' ABCD ';}} Foo::a (); Echo foo:: $a;  

We can also use the static keyword to limit the value of a variable at this time when the variable does not keep the last negative.

static inheritance and use

Class Foo{public static $a;p ublic static function A () {  return ' ABCD ';}} Class Oo extends foo{public static  function A () {  echo ' 1234 ';}} Oo::a ();  

Static inheritance is the same as the normal class inheritance method, and it is not much different.

Class Foo{public static $a;p ublic 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 the parent:: Method to refer to the parent class method, and does not directly self:: To use the parent class method, as the following example, will not display any value:

Class Foo{public static $a;p ublic static function A () {  return ' ABCD ';}} class Oo extends foo{public static  funct Ion AA () {  echo self::a ();}} Oo::a ();  

One more simple example.

 
  ?
 
    

http://www.bkjia.com/PHPjc/752510.html www.bkjia.com true http://www.bkjia.com/PHPjc/752510.html techarticle As we all know, the purpose of OOP is to write code once, copy and copy, inherit and inherit the reasonable use of every work, but also convenient code management. Which ring ...

  • 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.