Sample code sharing for PHP using range resolution operators

Source: Internet
Author: User
In object-oriented programming, some of its own operators, such as-&GT, are used to access its own members in an object. The other one is the scope resolution operator: Two colons joined together (::). This symbol is used to access members in the class, not in the object. Use the following methods:

Classname::methodname (); ClassName::p ropertyname;

This type of structure may be used in two places:

1. When using a class, the parent and child classes have the same properties and methods, which can be used to avoid confusion.

2. Outside the class, use this operator to access the members of the class without creating an object.

As we can use $this in a class to refer to an instance of the current object, the keyword self is used as a reference to the current class.

Class SomeClass {    function construct () {self        ::d O ();    }    protected function do () {        echo "done!";    }}

In this code, self::d O () triggers the Do () method of the current class.

To reference a member of a parent class, you can use the keyword parent and scope resolution operators to reference:

Class SomeOtherClass extends SomeClass {    function construct () {        parent::d O ();    }}

In most cases, we use the scope resolution operator to access the overridden method. We can also use it to access static and constant members.

Note: Like a class constant and a static property, it can be accessed by all instances of the class (or its subclasses). However, its value cannot be changed. Creating a class constant is using the const keyword, followed by a constant name (no dollar sign). Constants are not accessible through objects, such as $obj->pi or $obj::P I are not, but we can use classname::constant_name anywhere. You can also use Self::constant_name in methods in the class.

Sample program:

<?php  class Rectangle {protected static $_count = 0;protected $width;p rotected $height; function construct ($width , $height) {$this->width = $width; $this->height = $height; Self::$_count++;echo "created successfully". Self::$_count. " A rectangle object <br> ";} function destruct () {echo "destroys a Rectangle object <br>";} function Getarea () {echo "Rectangle area is:". ( $this->width * $this->height. " <br> ");} function Getconunt () {return self::$_count;}} Class Square extends Rectangle {function construct ($side) {$this->width = $side; $this->height = $side;p arent::$_ Count++;echo "successfully created". Parent::$_count. " A rectangle (Square) object <br> ";}} $rec = new Rectangle (5); $rec->getarea (); $square = new Square; $square->getarea (); >

Operation Result:

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.