Php -- static keyword

Source: Internet
Author: User
This section describes how to use the static keyword to define static methods and attributes. Static variables can also be used to define static variables and post-static binding. If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a class (but static methods are acceptable ). To be compatible with PHP4, if no access control is specified, attributes and methods are considered public. This section describes how to use the static keyword to define static methods and attributes. Static variables can also be used to define static variables and post-static binding.

If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a class (but static methods are acceptable ).

To be compatible with PHP 4, if no access control is specified, attributes and methods are considered public.

Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.

Static attributes cannot be accessed by objects through the-> operator.

Calling a non-static method in static mode will result in an E_STRICT error.

Just like all other PHP static variables, static attributes can only be initialized as text or constants and cannot use expressions. Therefore, static attributes can be initialized as integers or arrays, but they cannot be initialized as another variable or function return value or point to an object.

Example #1 static property Example

class Foo{    public static $my_static = 'foo';    public function staticValue(){        return self::$my_static;    }}class Bar extends Foo{    public function fooStatic(){        return parent::$my_static;    }}print Foo::$my_static.'
';$foo = new Foo();print $foo->staticValue().'
';print $foo->my_static.'
';print $foo::$my_static.'
';$classname = 'Foo';print $classname::$my_static;print Bar::$my_static.'
';$bar = new Bar();print $bar->fooStatic().'
';

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.