Php--static keywords

Source: Internet
Author: User
This page describes defining static methods and properties with the static keyword. Static can also be used to define statically variable and late static bindings.

Declaring a class property or method as static can be accessed directly without instantiating the class. Static properties cannot be accessed through an object that a class has instantiated (but static methods can).

To be compatible with PHP 4, if no access control is specified, the properties and methods default to public.

Because static methods do not need to be called through an object, the pseudo-variable $this is not available in a static method.

A static property cannot be accessed by an object through the operator.

Invoking a non-static method in a static manner results in an e_strict level error.

Just like all other PHP static variables, static properties can only be initialized to literals or constants, and expressions cannot be used. You can initialize a static property to an integer or an array, but you cannot initialize it to 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. ' <br> '; $foo = new Foo ();p rint $foo->staticvalue (). ' <br> ';p rint $foo->my_static. ' <br> ';p rint $foo:: $my _static. ' <br> '; $classname = ' Foo ';p rint $classname:: $my _static;print Bar:: $my _static. ' <br> '; $bar = new bar ();p rint $bar->foostatic (). ' <br> ';
  • 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.