Application of double colon in PHP

Source: Internet
Author: User
Tags access properties getcolor vars
Application of double colon in PHP

The PHP class code often sees the "::" operator, which is scoped to the scope operator, and is represented by a double-colon "::", which is used to pin the levels of different scopes in the class. The left side is the member of the access scope to the right of the scope.

The scopes defined in PHP are self and parent two (a static scope is provided in PHP6).

Self: Represents the scope of the current class, unlike this, which does not represent a particular instance of a class, does not work in code other than the class, and it does not recognize its position in the hierarchy of inheritance. That is, when you use self in an extension class, it calls a method other than the parent class, but instead extends the overloaded method of the class.


Parent: Represents the scope of the current class parent class, and the rest is the same as the self attribute.


Example PHP double colon:: operator:

?

PHP code

  1. class ? forasp{ ??
  2. ?? Static ? $url = "Http://blog.csdn.net/abandonship" ; ??
  3. ?? Static ? $webname ?=? "PHP Learn the use of double colons" ; ??
  4. ?? Public ? function ? Writeurl () {??
  5. ???? Echo ? Self:: $url ; //Call your own content ??
  6. ??} ??
  7. ?? Public ? function ? Writewebname () {??
  8. ???? Echo ? "Test subclass invokes Parent class content" ; ??
  9. ??} ??
  10. } ??
  11. ??
  12. class ? cn? extends ? forasp{ ??
  13. ?? function ? Father () {??
  14. ???? Parent::wirtewebname ();??
  15. ??} ??
  16. } ??
  17. ??
  18. $a ?=? New ? forasp (); //Instantiate parent class ??
  19. $a ->writeurl (); //Call own content ??
  20. $b ?=? New ? CN ();? ?
  21. $b ->writewebname (); //Call the parent class content ??
  22. ???

?

You can also use:: To invoke static methods or properties in a class in a call to a static method, which reduces resource usage because instances of each class occupy a portion of the resource.


Php6 in the static:: Scope, is we no longer need self:: and Parent::. When you want a class that points to the final implementation functionality, use static: This qualifier calculates the members of the last class in the inheritance layer immediately before the code executes, a process called deferred binding.

The double-colon operator, or scopeResolution Operator, can access properties and methods overridden in static, const, and class classes, or also known as scope-qualified operators.
Used outside of the class definition, called with the class name. In PHP 5.3.0, you can use variables instead of class names.


Program List: Accessing the class definition externally with a variable

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::d Oublecolon ();??
  13. ???

?

Program Run Result:

Fruit Color Red

Program List: Calling 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 Run Result:
Fruit::showcolor ()
Apple::showcolor ()

?

Program List: Using scope qualifiers

?

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 Run Result:
Banana is yellow

Program List: Methods for calling base classes

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 Run Result:
Show Color

  • 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.