PHP Object-oriented operator ':: ' Usage brief

Source: Internet
Author: User
What is the operator "::"?

Operator "::" is more powerful than pseudo-variable $this only in the internal use of the class. The operator "::" can access a member method in a class without any declaration of any instance of a live member variable.

Operator "::" Syntax:

The general syntax for using the "::" operator is:

Keyword:: variable name/constant name/method name


Precautions:

The keywords here are no longer public, private, protected or static, but the following three scenarios:

(1) Parent: You can call member variables, member methods, and constants in the parent class;

(2) Self: can invoke static members and constants in the current class;

(3) Class name: You can call variables, constants, and methods in this class.

Friendly reminder:

For more information about static variables (methods), please refer to the article on this site: http://www.php.cn/php-weizijiaocheng-360326.html

The above simple introduction of the following operator "::" The basic content, below we have a simple example to more specific understanding of the operator "::" usage.

<?phpheader ("Content-type:text/html;charset=utf-8"); class hero{                                        //define class    Const CHARACTER = ' Anna ';                  Define constants, using the keyword const function    __construct ()                        //constructor in parent class    {        echo ' 5 floor to play in single '. Hero:: CHARACTER. ' Send it without giving ';     Output constant        echo ' <br/> ';    }} class I_hreo extends hero{                      //Create subclass I_hreo Inherit parent class     const ROLE = ' ya ';                         Create a const     function __construct ()                      //subclass in Create constructor     {         Parent:: __construct ();               Referencing a constructor in a parent class         echo ' 4 floor in single '. Self:: ROLE. ' Thief 6 '; Sub-class Output     }} $gamer = new I_hreo ();                             Instantiation of


Example Explanation:

In the above example, we first created a parent class hero, defined the constants, created the constructors, and then created a subclass I_hreo to inherit the parent class Hreo, and also defined constants in the subclass. So here's the point, we're referencing the constructors in the parent class. We've covered three cases, parent: You can call member variables, member methods, and constants in the parent class. Here we call the methods in the parent class, so use the parent:: __construct (). Next, we output the constants defined in the subclass in the constructor method of the subclass, invoke the keyword of the current class, as described above, self: can call static members and constants in the current class, so here we use self:: ROLE, finally formatted, run a bit.

Results See:

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.