The difference and use of const and static in PHP

Source: Internet
Author: User
Tags php class
This article will explain the difference and use of const and static in PHP.

The first thing about const inside a PHP class is that you can only modify the member properties, not the method, as follows:

Class test{     Const PATH = ' c/';//modifier constant     const function Test () {//This method is wrong, const cannot modify the method     }}

Const does not need to add a permission modifier field when it is used, but it is tricky to invoke const-decorated constants, because constants belong to the entire class, not to an object, so you need to use the class name plus the domain name modifier when calling, for example:

echo Test::P ath; You can also call $test = new test () using the following method: echo  $test::P ath;

However, when called inside the class, there is a problem, how to get the current class name or the current object name within the class (PHP can get the class name-reflection from the object), PHP provides us with two special keywords, such as:

Class test{     Const PATH = ' c/';//modifier constant public     function () {         echo $this::P ath. ' <br/> ';         echo Self::P ath. ' <br/> ';     } }

where self (without $) represents the current class name, $this represents the current object. This allows us to invoke const-decorated constants inside the class.

Then we'll talk about static.

Although the const is very useful, but once the definition can not modify the value, but sometimes we need a property belonging to the class, but can also modify its value, such as the statistics of this class constructs a few objects, this is the time to use static, when the static modification of the class's member variables, It uses the same method as the const, the difference is that the static modified properties can add permissions (7.1 in PHP can be added to the const, the previous cannot), and the static modified member variable value can be modified.

But when static modifies the member method, the use of the method changes a little, inside the class, inside the static modified method body, unable to access any ordinary member variables of the current class, it is accurate to use the keyword $this, can only use the current class static member variables, and class constants.

Class test{     static public $name = ' ASD ';     constπ= 3.1415926;     Public $age = ten;     public static function Lenth ($r) {         //echo $this->age;//error, cannot access normal member variable         echo Test:: $name;         return $r *2*self::π;         }     public static function area ($r) {         return $r * $r *self::π;     }    }

In the process of using static, the method called is different from the variable that calls the static adornment, either by the class name or by the object name, such as:

echo ' perimeter: '. Test::lenth (1). ' <br/> '; Echo ' Area: '. Test::area (1). ' <br/> '; $per = new Test (); Echo $per->lenth (3);

Both of these invocation methods are legal.

This article explains the difference and use of const and static in PHP, and more about the PHP Chinese web.

Related illustrations:

The use of PHP and its regular expressions is detailed

How does PHP automatically load the two functions __autoload and apl_autoload_register?

Error handling and implementation method of PHP

Related Article

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.