In object-oriented programming, some of its own operators, such as->, 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: