Php final static const member attribute usage

Source: Internet
Author: User
Tags constant php tutorial

First, let's talk about final.
1. final cannot be used to modify member attributes.
2. final can only modify classes and methods
Role: The Modified class cannot inherit from the quilt class; the modified method cannot overwrite the method of the quilt class.

<? PHP

Final class BaseClass {

Public function test (){

Echo "BaseClass: test () calledn ";

Final public function moreTesting (){

Echo "BaseClass: moreTesting () calledn ";

Class ChildClass extends BaseClass {

Public function moreTesting (){

Echo "ChildClass: moreTesting () calledn ";

// Results in Fatal error: Cannot override final method BaseClass: moreTesting ()

?>


Then let's talk about static.
1. static is used to modify member attributes and member methods, but cannot be used to modify classes.
2. The member attributes modified with static can be shared by all objects of the same class.
3. Static data is stored in the data segment in the memory (Initialize the static segment)
4. Static data is allocated to the memory when the class is loaded for the first time.
5. What is a class loaded? As long as this class is used in the program (this class name appears)
6. Static member attributes must be accessed by class name, without creating objects or accessing objects. In the class, self can be used to represent the class ($ this)
7. static methods cannot access non-static member attributes (but static member attributes can be accessed in non-static methods). Non-static member attributes must be accessed by objects, to access non-static member attributes internally, use $ this.
8. If you confirm that a method does not need to use non-static member attributes, you can declare this method as a static method and access it without instantiating an object.

Example: an example of static variables

<? PHP
Function Test ()
   {
$ W3sky = 0;
Echo $ w3sky;
$ W3sky ++;
   }
?>

Static variables and recursive functions

<? PHP
Function Test ()
   {
Static $ count = 0;
  
$ Count ++;
Echo $ count;
If ($ count <10 ){
Test ();
   }
$ Count --;
   }
?>

Note: static variables can be declared according to the preceding example. If a value is assigned to the expression result in the declaration, the parsing error occurs.

Static variables declared in the example

<? PHP
Function foo (){
Static $ int = 0; // correct
Static $ int = 1 + 2; // wrong (as it is an expression)
Static $ int = sqrt (121); // wrong (as it is an expression too)
  
$ Int ++;
Echo $ int;
   }
?>


Finally, const
1. It can only be used to modify member attributes
2. The const must be used to declare constants in the class.
3. The access method is the same as the access to static member attributes (class name: member attribute is used outside the class, and self: member attribute is used in the class)
4. Constants must be declared with the initial value.

<? Php Tutorial
Class say_const {
Const CHARSET = "China ";
Publice function say_hello (){
Echo slef: CHARSET;
}
}
$ Const1 = new say_const ()'
$ Const1-> say_hello ();
?>

Constants can only contain scalar data (boolean, integer, float, and string). Do not define resource constants.
You can use the constant () function to read the value of a constant. get_defined_constants () to obtain a list of all defined constants.
If an undefined CONSTANT is used, PHP assumes that it wants the name of the CONSTANT, just like calling it with a string (CONSTANT corresponds to "CONSTANT "), an E_NOTICE error is reported.

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.