Static and declared constants in the class of PHP

Source: Internet
Author: User

The static keyword is static,static is a modifier that modifies the class's behavior and the class's object, and can be called directly by the class name, in addition to being called by the object.

* * Constants
$this can only access variables and functions in a class
Constants in classes that are accessed within a class can only use the Self keyword
If you need to directly access a constant in a class outside of the class, use the class name +::, or you can use the instantiated object of the class to access the constants in the class
* * Static variables
* Static variables are variables that exist only at the function scope
static variables are more special, It is placed in the static area of the computer's memory
example: starting from PHP5.3.0 to use the following access method
echo $ Test:: $num;
$test:: $num = $;
echo ' changed static variable: '. $test:: $num;
** static method
non-static functions can call static function methods directly
external access to static methods: Use class name + range resolution operator + static function method name
example: MyObject:: DESC2 ();

$a->desc2 (); Such a way to access static resources, not recommended

For example:

Static and declared constants in the class of PHP

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.