PHP sample code sharing using range resolution operator

黄舟
Release: 2023-03-07 07:56:01
Original
1685 people have browsed it

Object-orientedProgramming will use some of its own operators, such as ->, this symbol is used Access its own members within an object. The other one is the range resolution operator: two colons connected together (::). This notation is used to access members within a class (not within an object). The usage is as follows:

This structure may be used in two places:

1. When using a class, the parent class and the subclass have the same

attributes and Use this to avoid confusion when using method

. 2. When outside the class, use this operator to access the members of the class without

creating an object

. Just as we can use $this in a class to

reference

the instance of the current object, the keyword self is used as a reference to the current class.

In this code, self::do() will trigger the do() method of the current class.

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

class SomeOtherClass extends SomeClass {
    function construct() {
        parent::do();
    }
}
Copy after login

In most cases, we use the range resolution operator to access the repeated method of writing. We can also use it to access

static

and constant members. Note: Like static properties, class constants can be accessed by all instances of the class (or its subclasses). But its value cannot be changed. Class constants are created using the

const keyword

, followed by the constant name (without the dollar sign). Constants cannot be accessed through objects, such as $obj->PI or $obj::PI, but we can use ClassName::CONSTANT_NAME anywhere. You can also use self::CONSTANT_NAME in methods within a class. Sample program:

<?php  
	class Rectangle {
		protected static $_count = 0;
		protected $width;
		protected $height;

		function construct($width, $height) {
			$this->width = $width;
			$this->height = $height;
			self::$_count++;
			echo "已成功创建".self::$_count."个Rectangle对象<br>";
		}

		function destruct() {
			echo "销毁一个Rectangle对象<br>";
		}

		function getArea() {
			echo "Rectangle面积是:".($this->width * $this->height."<br>");
		}

		function getConunt() {
			return self::$_count;
		}
	}

	class Square extends Rectangle {
		function construct($side) {
			$this->width = $side;
			$this->height = $side;
			parent::$_count++;
			echo "已成功创建".parent::$_count."个Rectangle(Square)对象<br>";
		}
	}

	$rec = new Rectangle(10, 5);
	$rec->getArea();

	$square = new Square(10);
	$square->getArea();

?>
Copy after login

Running results:

The above is the detailed content of PHP sample code sharing using range resolution operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!