PHP static methods and attributes

Source: Internet
Author: User
: This article mainly introduces PHP static methods and attributes. For more information about PHP tutorials, see.
class hw{public static function hi(){return 'Hello World';}}echo hw::hi();//Output:Hellp Worldclass hw2{public function hi(){return 'Hello Wrold';}}echo hw2::hi();//Output:Hellp World

As shown in the preceding example, both static and non-static attributes can be used directly: The method is called directly from the outside. However, static is recommended for efficiency and other factors.

Static class internal call method

class foo{    private static function c()    {        return 'abcde';    }    public static function a()    {        echo self::c();    }    public static function b()    {        echo $this->c();    }    public function  e()    {        echo self::c();    }}foo::a();//Output:abcdefoo::b();//Output:Fatal error: Using $this when not in object context infoo::e();//Output:abcef

Methods restricted by the static keyword must use self: which is referenced inside the class.

Static property

class foo{public static $a;public static function a(){self::$a = 'abcd';}}foo::a();//Output:abcdeecho foo::$a;

Static inheritance and use

class foo{public static $a;public static function a(){return 'abcde';}}class soo extends foo{public static function a(){echo '12345';}}soo::a();//Output:12345
The inheritance of static is the same as that of ordinary classes.

class foo{public static $a;public static function a(){return 'abcde';}}class soo extends foo{public static function a(){echo parent::a();}}soo::a();//Output:12345

There is a write difference when using it. it must be the parent: Method to reference the method of the parent class, and cannot be used directly by self:. otherwise, no output will be provided:

class foo{public static $a;public static function a(){return 'abcd';}} class soo extends foo{public static  function aa(){echo self::a();}}soo::a();  

The above describes PHP static methods and attributes, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.