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
-
- class ? forasp{ ??
- ?? Static ? $url = "Http://blog.csdn.net/abandonship" ; ??
- ?? Static ? $webname ?=? "PHP Learn the use of double colons" ; ??
- ?? Public ? function ? Writeurl () {??
- ???? Echo ? Self:: $url ; //Call your own content ??
- ??} ??
- ?? Public ? function ? Writewebname () {??
- ???? Echo ? "Test subclass invokes Parent class content" ; ??
- ??} ??
- } ??
- ??
- class ? cn? extends ? forasp{ ??
- ?? function ? Father () {??
- ???? Parent::wirtewebname ();??
- ??} ??
- } ??
- ??
- $a ?=? New ? forasp (); //Instantiate parent class ??
- $a ->writeurl (); //Call own content ??
- $b ?=? New ? CN ();? ?
- $b ->writewebname (); //Call the parent class content ??
- ???
?
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
-
- class ? Fruit? { ??
- ?? Const ? Const_value?=? ' Fruit? Color ';??
- } ??
- $classname ?=? ' Fruit ' ; ??
- Echo ? $classname :: const_value;? //? As?of? php?5.3.0 ??
- Echo ? fruit::const_value;??
- ???
?
Program List: Use double colons outside the class definition (::)
PHP code
-
- class ? Fruit? { ??
- ?? Const ? Const_value?=? ' Fruit? Color ';??
- } ??
- class ? Apple? extends ? Fruit??
- { ??
- ?? Public ? Static ? $color ?=? ' Red ' ; ??
- ?? Public ? Static ? function ? Doublecolon ()? { ??
- ???? Echo ? parent::const_value?.? "\ n" ; ??
- ???? Echo ? Self:: $color ?.? "\ n" ; ??
- ??} ??
- } ??
- Apple::d Oublecolon ();??
- ???
?
Program Run Result:
Fruit Color Red
Program List: Calling the parent method
?
PHP code
-
- class ? Fruit??
- { ??
- ???? protected ? function ? Showcolor ()? { ??
- ???????? Echo ? "Fruit::showcolor () \ n" ; ??
- ????} ??
- } ??
- ??
- class ? Apple? extends ? Fruit??
- { ??
- ???? //? Override?parent ' s?definition ??
- ???? Public ? function ? Showcolor ()? ?
- ???? { ??
- ???????? //? But?still?call?the?parent?function ??
- ???????? Parent::showcolor ();??
- ???????? Echo ? "Apple::showcolor () \ n" ; ??
- ????} ??
- } ??
- ??
- $apple ?=? New ? Apple ();??
- $apple ->showcolor ();??
- ???
?
Program Run Result:
Fruit::showcolor ()
Apple::showcolor ()
?
Program List: Using scope qualifiers
?
PHP code
-
- ???? class ? Apple??
- ???? { ??
- ???????? Public ? function ? Showcolor ()? ?
- ???????? { ??
- ???????????? return ? $this ->color;??
- ????????} ??
- ????} ??
- ???? class ? Banana??
- ???? { ??
- ???????? Public ? $color ; ??
- ???????? Public ? function ? __construct ()? ?
- ???????? { ??
- ???????????? $this ->color?=? "Banana?is?yellow" ; ??
- ????????} ??
- ???????? Public ? function ? GetColor ()??
- ???????? { ??
- ???????????? return ? Apple::showcolor ();??
- ????????} ??
- ????} ??
- ???? $banana ?=? New ? Banana;??
- ???? Echo ? $banana ->getcolor ();??
- ???
?
Program Run Result:
Banana is yellow
Program List: Methods for calling base classes
PHP code
-
- ??
- class ? Fruit??
- { ??
- ???? Static ? function ? Color ()??
- ???? { ??
- ???????? return ? "Color" ; ??
- ????} ??
- ??
- ???? Static ? function ? Showcolor ()? ?
- ???? { ??
- ???????? Echo ? "Show?" ?.? Self::color ();??
- ????} ??
- } ??
- ??
- class ? Apple? extends ? Fruit??
- { ??
- ???? Static ? function ? Color ()??
- ???? { ??
- ???????? return ? "Red" ; ??
- ????} ??
- } ??
- ??
- Apple::showcolor ();??
- //?output?is? " Show?color "! ??
- ??
- ???
?
Program Run Result:
Show Color