How to define class constants in PHP (with code)

Source: Internet
Author: User
Tags php software
This article brings you the content is about how PHP through the path to get the file name (code), there is a certain reference value, the need for friends can refer to, I hope to help you.

Definition of a class constant

A constant defined inside a class.

Class constants must be defined with the Const keyword

Const constant NAME = value;

The relationship of constants to objects

Constants cannot be accessed through objects

access to class constants

A class constant belongs to a class and not to an object. object is not accessible to class constants.

Constant access must be achieved through the class:

Class Name:: Constant Name

:: Called Range resolution operator

There is a constant use of classes within the class to access the members of the class, when the class is accessed internally using the class, there is a keyword similar to $this that represents the object: self, its own meaning, represents the current class name

<?phpclass person () {    const haoyun= ' PHP software Development Engineer ';} Get $person=new person () from outside the class, echo $person->haoyun;echo Haoyun;echo person::haoyun;//class internal get
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.