Application of double colons in php

Source: Internet
Author: User
Tags getcolor
In php, double colons are often seen in php Code: operator. this is the scope limitation operator, which is represented by a double colon:. it is used to pin the levels of different scopes in the class. On the left is the access scope member on the right of the scope. The scope defined in php is self and parent (static scope is provided in php6 ). Self: indicates the scope of the current class. Unlike this, it does not indicate the application of double colons in a specific php instance of the class.

In php code, we often see the ":" operator, which is a limited scope operator. it is represented by a double colon ":", which is used to pin the levels of different scopes in the class. On the left is the access scope member on the right of the scope.

The scope defined in php is self and parent (static scope is provided in php6 ).

Self: indicates the scope of the current class. Unlike this, self does not represent a specific instance of the class and cannot be used in code outside the class, in addition, it cannot identify its position in the hierarchy of inheritance. That is to say, when self is used in an extension class, it calls not the method of the parent class, but the method of overloading the extension class.


Parent: indicates the scope of the current class parent class, and the rest are the same as the self feature.


For example, php double colon: operator:

?

Php code

  1. Class? Forasp {??
  2. ??Static? $ Url = "http://blog.csdn.net/abandonship ";??
  3. ??Static? $ Webname? =? "Usage of double colons in PHP learning ";??
  4. ??Public?Function? Writeurl (){??
  5. ???? Echo? Self: $ url; // call your own content ??
  6. ??} ??
  7. ??Public?Function? Writewebname (){??
  8. ???? Echo? "Test subclass calling parent class content ";??
  9. ??} ??
  10. }??
  11. ??
  12. Class? Cn?Extends? Forasp {??
  13. ??Function? Father (){??
  14. ???? Parent: wirtewebname ();??
  15. ??} ??
  16. }??
  17. ??
  18. $? =?New? Forasp (); // instantiate the parent class ??
  19. $ A-> writeurl (); // call its own content ??
  20. $ B? =?New? Cn ();??
  21. $ B-> writewebname (); // call the content of the parent class ??
  22. ?> ??

?

You can also use: to call static methods or attributes in a class to reduce resource usage, because each class instance occupies part of the resource.


In php6, static: scope is proposed. we no longer need self: And parent ::. When you want to point to the class that implements the final function, use static:. this qualifier will calculate the last class member in the inheritance layer immediately before the code is executed, this process is called delayed binding.

The "double colon Operator", or "Scope Resolution Operator", can access static, const, and attributes and methods that are rewritten in a class.
Use the class name for calling when it is used outside the class definition. In PHP 5.3.0, you can use variables instead of class names.


Program List: use variables to define external access in the class

Php code

  1. Class? Fruit? {??
  2. ??Const? CONST_VALUE? =? 'Fruit? Color ';??
  3. }??
  4. $ Classname? =? 'Fruit ';??
  5. Echo? $ Classname: CONST_VALUE ;? //? As? Of? PHP? 5.3.0 ??
  6. Echo? Fruit: CONST_VALUE ;??
  7. ?> ??

?

Program List: use double colons (: :) outside the class definition (::)

Php code

  1. Class? Fruit? {??
  2. ??Const? CONST_VALUE? =? 'Fruit? Color ';??
  3. }??
  4. Class? Apple?Extends? Fruit ??
  5. {??
  6. ??Public?Static? $ Color? =? 'Red ';??
  7. ??Public?Static?Function? DoubleColon ()? {??
  8. ???? Echo? Parent: CONST_VALUE ?.? "\ N ";??
  9. ???? Echo? Self: $ color ?.? "\ N ";??
  10. ??} ??
  11. }??
  12. Apple: doubleColon ();??
  13. ?> ??

?

Program running result:

Fruit Color Red

Program List: call the parent method

?

Php code

  1. Class? Fruit ??
  2. {??
  3. ????Protected?Function? ShowColor ()? {??
  4. ???????? Echo? "Fruit: showColor () \ n ";??
  5. ????} ??
  6. }??
  7. ??
  8. Class? Apple?Extends? Fruit ??
  9. {??
  10. ???? //? Override? Parent's? Definition ??
  11. ????Public?Function? ShowColor ()??
  12. ???? {??
  13. ???????? //? But? Still? Call? The? Parent? Function ??
  14. ???????? Parent: showColor ();??
  15. ???????? Echo? "Apple: showColor () \ n ";??
  16. ????} ??
  17. }??
  18. ??
  19. $ Apple? =?New? Apple ();??
  20. $ Apple-> showColor ();??
  21. ?> ??

?

Program running result:
Fruit: showColor ()
Apple: showColor ()

?

Program List: Use the scope qualifier

?

Php code

  1. ????Class? Apple ??
  2. ???? {??
  3. ????????Public?Function? ShowColor ()??
  4. ???????? {??
  5. ????????????Return? $ This-> color ;??
  6. ????????} ??
  7. ????} ??
  8. ????Class? Banana ??
  9. ???? {??
  10. ????????Public? $ Color ;??
  11. ????????Public?Function? _ Construct ()??
  12. ???????? {??
  13. ???????????? $ This-> color? =? "Banana? Is? Yellow ";??
  14. ????????} ??
  15. ????????Public?Function? GetColor ()??
  16. ???????? {??
  17. ????????????Return? Apple: showColor ();??
  18. ????????} ??
  19. ????} ??
  20. ???? $ Banana? =?New? Banana ;??
  21. ???? Echo? $ Banana-> GetColor ();??
  22. ?> ??

?

Program running result:
Banana is yellow

Program List: call the method of the base class

Php code

  1. ??
  2. Class? Fruit ??
  3. {??
  4. ????Static?Function? Color ()??
  5. ???? {??
  6. ????????Return? "Color ";??
  7. ????} ??
  8. ??
  9. ????Static?Function? ShowColor ()??
  10. ???? {??
  11. ???????? Echo? "Show? "?.? Self: color ();??
  12. ????} ??
  13. }??
  14. ??
  15. Class? Apple?Extends? Fruit ??
  16. {??
  17. ????Static?Function? Color ()??
  18. ???? {??
  19. ????????Return? "Red ";??
  20. ????} ??
  21. }??
  22. ??
  23. Apple: showColor ();??
  24. //? Output? Is? "Show? Color "! ??
  25. ??
  26. ?> ??

?

Program running result:
Show color

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.