PHP object-oriented programming and other constant usage examples, sed usage example _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming and other constant usage examples, sed usage examples. PHP object-oriented programming and other constant usage examples. sed usage instance constants are very important concepts in PHP object-oriented programming, A strong understanding of constants helps to further improve constant usage instances such as PHP object-oriented programming, and sed usage instances

Class constants are a very important concept in PHP object-oriented programming. a strong grasp of class constants will help to further improve the level of PHP object-oriented programming. This article describes the usage of class constants in PHP programming as an example. The details are as follows:

Class constant: in the class, data that remains unchanged during the running period is saved.

Definition:

Const keyword const constant name = constant value

Example:

Class Student {public $ stu_id; public $ stu_name; public $ stu_gender; const GENDER_MALE = 'male'; const GENDER_FEMALE = 'femal ';}

Class constants are not restricted by access modifiers.
Access method:
Class: constant name

Example:

Class Student {public $ stu_id; public $ stu_name; public $ stu_gender; const GENDER_MALE = 'male'; const GENDER_FEMALE = 'feme'; public function _ construct ($ id, $ name, $ gender = '') {$ this-> stu_id = $ id; $ this-> stu_name = $ name; $ this-> gender = ($ gender = '')? Self: GENDER_MALE: $ gender ;}}

Summary:The following elements can be defined in a class: constants, static attributes, non-static attributes, static methods, and non-static methods..

Note:
$ This indicates the current object. does it always represent the object of the class where $ this is located?
The answer is No! Because the value of $ this does not depend on the class where $ this is located, but on the execution object (execution environment) when the $ this method is called)

The execution environment of the method. The $ this in the method indicates the object in which the current method is executed.


What is php object-oriented programming?

Object-oriented Programming (OOP) is designed to create software to reuse code and better simulate real-world environments, this makes it recognized as the winner of top-down programming. It adds an extension statement to the program to encapsulate the function into the "object" necessary for programming. The object-oriented programming language makes complex work organized and easy to write

What problems does final constants occur in object-oriented programming?

Final constants indicate that they cannot be modified, that is, they are equivalent to constants in C language.

The final function is not allowed to be overwritten after being inherited.

If the final ID is not used, no error will be reported if it is changed, and the stability of the program will be affected.

Class constants are a very important concept in PHP object-oriented programming. it is helpful to grasp class constants...

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.