PHP中::、->、self、$this操作符
在访问PHP类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::,反之如果被引用的变量或者方法没有被声明成const或者static,那么就必须使用操作符->.
另外,如果从类的内部访问const或者static变量或者方法,那么就必须使用自引用的self,反之如果从类的内部访问不为const或者static变量或者方法,那么就必须使用自引用的$this.
$this实例代码如下:
<?php // this是指向当前对象的指针 class test_this { private $content; //定义变量 function __construct($content) { //定义构造函数 $this->content = $content; } function __destruct() { } //定义析构函数 function printContent() { //定义打印函数 echo $this->content . '<br />'; } } $test = new test_this('北京欢迎你!'); //实例化对象 $test->printContent(); //北京欢迎你! ?>
::使用方法实例代码如下:
<?php //parent是指向父类的指针 class test_parent { //基类 public $name; //定义姓名 父类成员需要定义为public,才能够在继承类中直接使用 this来调用. function __construct($name) { $this->name = $name; } } class test_son extends test_parent { //派生类 继承test_parent public $gender; //定义性别 public $age; //定义年龄 function __construct($gender, $age) { //继承类的构造函数 parent::__construct('nostop'); //使用parent调用父类的构造函数,来进行对父类的实例化 $this->gender = $gender; $this->age = $age; } function __destruct() { } function print_info() { echo $this->name . '是个' . $this->gender . ',今年' . $this->age . '岁' . '<br />'; } } $nostop = new test_son('女性', '22'); //实例化test_son对象 $nostop->print_info(); //执行输出函数 nostop是个女性,今年23岁 ?>
使用self::$name的形式.注意的是const属性的申明格式,const PI=3.14,而不是const $PI=3.14
实例代码如下:
<?php class clss_a { private static $name = "static class_a"; const PI = 3.14; public $value; public static function getName() { return self::$name; } //这种写法有误,静态方法不能访问非静态属性 public static function getName2() { return self::$value; } public function getPI() { return self::PI; } } ?>
还要注意的一点是如果类的方法是static的,他所访问的属性也必须是static的.
在类的内部方法访问未声明为const及static的属性时,使用$this->value ='class_a';的形式.
文章地址:
转载随意^^请带上本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

const is a keyword that can be used to declare constants, const modifiers in function parameters, const modified function return values, and const modified pointers. Detailed introduction: 1. Declare constants. The const keyword can be used to declare constants. The value of the constant cannot be modified during the running of the program. The constant can be a basic data type, such as integer, floating point number, character, etc., or a custom data type; 2. The const modifier in the function parameters. The const keyword can be used in the parameters of the function, indicating that the parameter cannot be modified inside the function, etc.

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

For C++ programmers, syntax errors are one of the most common problems. One of the common mistakes is that const objects must be initialized at definition time. If you encounter this situation, how should you deal with it? First, we need to understand what a const object is. The const keyword is a special type qualifier in C++ that specifies that the value of a variable cannot be changed during the execution of the program. Such variables are called "constants". If you define a const object without initializing it, you will encounter the above error. This is

Correct usage of the const keyword in C++: Using const to modify a function means that the function will not modify the parameters or class members passed in. Using const to declare a function pointer means that the pointer points to a constant function.

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.
