Basic php knowledge: Class and object (5) static

Source: Internet
Author: User
Tags php basics
Php Basics: class and object (5) static Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can ).
Declared static class variables and methods can be called without the need to instantiate class objects. Static classes cannot be called by class objects. (The static method of the class is acceptable ). // Note: In the first example, a static variable is called in a non-static method. The only difference is that self is used. Can we use self ???? Don't know ??? A test is required.

The static declaration must be after the visibility declaration. For compatibility with PHP4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.
The static statement must be an explicit statement. To be compatible with PHP4, if no explicitly declared object or method exists, it is declared as public.

Because static methods are callable without an instance of the object created, the pseudo variable $ this is not available inside the method declared as static.
Because static methods do not need to be called by instantiating class objects, the pseudo variable $ this is also unavailable in static methods.

In fact static method callare resolved at compile time. when using an explicit class name the method is already identified completely and no inheritance rules apply. if the call is done by self then self is translated to the current class, that is the class the code belongs. here also no inheritance rules apply.
In fact, static method calls are determined at compilation. (I will not translate this section .??? Don't understand ???)
For a long time, the translation is as follows:
------------------------------------------------
In fact, static method calls are solved during compilation. When a clear class name is used, the method has been fully recognized without applying inheritance rules. If it is called by itself, it is parsed into the current class, that is, the class to which the code belongs. There are no application inheritance rules here.
But a new problem:
Inheritance is not necessarily generated here. why do we mention inheritance rules? (??? Don't understand ????)

Static properties cannot be accessed through the object using the arrow operator->. Calling non-static methods statically generates an E_STRICT level warning.
Static members cannot be called by class objects through the arrow symbol>. Calling a non-static method statically results in an E_STRICT warning.

Static Member example:

The code is as follows:

Class Foo
{
Public static $ my_static = 'foo ';
Public function staticValue (){
Return self: $ my_static; // note here !!!!
// Return $ my_static; // whether the write fails. Need to test
}
}

Class Bar extends Foo
{
Public function fooStatic (){
Return parent: $ my_static; // pay attention to this !!!!
}
}
Print Foo: $ my_static. "n ";
$ Foo = new Foo ();
Print $ foo-> staticValue (). "n ";
Print $ foo-> my_static. "n"; // undefined "Property" my_static
// $ Foo: my_static is not possible
Print Bar: $ my_static. "n ";
$ Bar = new Bar ();
Print $ bar-> fooStatic (). "n ";

Static method example:
Class Foo {
Public static function aStaticMethod (){
//...
}
}
Foo: aStaticMethod ();

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.