PHP Static Methods and properties

Source: Internet
Author: User
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 seen from the example above, the use of static and static properties can be used directly after the:: Method is called directly from outside. But for efficiency and other reasons, it is advisable to use static to limit it.

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 that use the static keyword to restrict must use self:: Referenced inside this class.

static property

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

static inheritance and use

Class Foo{public static $a;p ublic static function A () {return ' ABCDE ';}} Class Soo extends foo{public static function A () {echo ' 12345 ';}} Soo::a ();//output:12345
Static inheritance is no different from normal class inheritance methods.

Class Foo{public static $a;p ublic static function A () {return ' ABCDE ';}} Class Soo extends foo{public static function A () {echo parent::a ();}} Soo::a ();//output:12345

It is only used when there is a write difference, it must be the parent:: Method to reference the method of the parents, and can not directly self:: To use the method, otherwise there will be no output:

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

The above describes the PHP static methods and properties, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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