$ This, static, final, const, and self usage in php

Source: Internet
Author: User
This article mainly describes how to use the $ this, static, final, const, and self keywords in the php class. $ This indicates that the current instance is accessed through internal methods of the class.

This article mainly describes how to use the $ this, static, final, const, and self keywords in the php class.

$ This

$ This indicates the current instance. when the internal method access of the class is not declared as a const or static attribute, $ this-> value = 'phpernote'; is used. Common usage:

$ This-> attribute

$ This-> method

The instance code is as follows:

  1. Class MyClass {
  2. Private $ name;
  3. Public function _ construct ($ name ){
  4. $ This-> name = $ name;
  5. }
  6. Public function getname (){
  7. Return $ this-> name;
  8. }
  9. Public function printName (){
  10. Echo $ this-> getname ();
  11. }
  12. }
  13. $ Myclass = new MyClass ("I Like www.111cn.net ");
  14. $ Myclass-> printName (); // output: I Like www.111cn.net
  15. ?>

There are three methods to call the attributes and methods of the current class in the class: self, parent, and $ this. The difference between the three keywords is: self is used to direct to the current class; parent is used to point to the parent class of the current class. you can use this keyword to call the attributes and methods of the parent class. $ this is used to call its own attributes and methods in the class body.

Static

The keyword can be self (used when a static member is called within the class) the class name of the static member (used when a static member inside the class is called outside the class)

Declare a static variable as follows:

Static $ val = '';

Variables only exist in the function scope. after the function is executed, the value of the variable will not be lost and will only be initialized once. the expression cannot be used to initialize static variables, you do not need to replace global variables because they are easily accessed by all functions and thus are not suitable for maintenance.

Using static in a class has two main purposes: defining static members and defining static methods. Static members only keep the value of one variable. this value is valid for all instances. the code is as follows:

  1. Class MyObject {
  2. Public static $ myStaticVar = 0;
  3. Function myMethod (){
  4. Self: $ myStaticVar + = 2;
  5. Echo self: $ myStaticVar;
  6. }
  7. }
  8. $ Instance1 = new MyObject ();
  9. $ Instance1-> myMethod ();
  10. $ Instance2 = new MyObject ();
  11. $ Instance2-> myMethod ();
  12. // The result is printed 2 and 4 respectively.

The instance code is as follows:

  1. Class Book {
  2. Static $ num = 0;
  3. Public function showMe (){
  4. Echo "you are a drop". self: $ num. "guest ";
  5. Self: $ num ++;
  6. }
  7. }
  8. $ Book1 = new Book ();
  9. $ Book1-> showMe ();
  10. Echo"
    ";
  11. $ Book2 = new Book ();
  12. $ Book2-> showMe ();
  13. Echo"
    ";
  14. Echo "you are a drop". Book: $ num. "guest ";
  15. ?>
  16. // The result will be:
  17. // You are a visitor of 0
  18. // You are a visitor
  19. // You are two visitors

In addition, it should be noted that if the class method is static, the Accessed attribute must also be static.

Final

PHP final keywords can modify classes and methods in classes, but they have similar functions, that is, if you use final keywords to modify them, the modified class or method cannot be extended or inherited. You can only honestly reference it. If you use final before the class, this indicates that this class cannot use inheritance. if you use the PHP final keyword before the method, this indicates that this method cannot be overwritten. The truth is that simple. let's take a look at a simple example.

The final class and method cannot be inherited, and the method modified by this keyword cannot be overwritten. The general usage is as follows:

  1. Final class MyClass {// This class cannot be inherited
  2. Final function fun1 () {...} // this method will not be allowed to be overwritten
  3. }

The instance code is as follows:

  1. Final class BaseClass {
  2. Public function test (){
  3. Echo "BaseClass: test () calledn ";
  4. }
  5. Final public function moreTesting (){
  6. Echo "BaseClass: moreTesting () calledn ";
  7. }
  8. }
  9. Class ChildClass extends BaseClass {
  10. Public function moreTesting (){
  11. Echo "ChildClass: moreTesting () calledn ";
  12. }
  13. }
  14. // Results in Fatal error: Cannot override final method BaseClass: moreTesting ()
  15. ?>

Const

When you access the attributes declared as const and static by using the internal method of the class, you must use the self ::$ name method. the instance code is as follows:

  1. Class clss_a {
  2. Private static $ name = "static class_a ";
  3. Const PI = 3.14;
  4. Public $ value;
  5. Public static function getName (){
  6. Return self: $ name;
  7. }
  8. // This statement is incorrect. the static method cannot access non-static attributes.
  9. Public static function getName2 (){
  10. Return self: $ value;
  11. }
  12. Public function getPI (){
  13. Return self: PI;
  14. }
  15. }

Note that the declarative format of the const attribute is const PI = 3.14, rather than const $ PI = 3.14.

Self

Self indicates the class itself, pointing to the current class. It is usually used for static members, methods, and constants of the category class.

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.