php面向对象全攻略 (三)特殊的引用“$this”的使用
7.特殊的引用“$this”的使用
现在我们知道了如何访问对象中的成员,是通过“对象->成员”的方式访问的,这是在对
象的外部去访问对象中成员的形式,那么如果我想在对象的内部,让对象里的方法访问本对
象的属性,或是对象中的方法去调用本对象的其它方法这时我们怎么办?因为对象里面的所
有的成员都要用对象来调用,包括对象的内部成员之间的调用,所以在PHP 里面给我提供了
一个本对象的引用$this,每个对象里面都有一个对象的引用$this 来代表这个对象,完成对象
内部成员的调用,this 的本意就是“这个”的意思,上面的实例里面,我们实例化三个实例
对象$P1、$P2、$P3,这三个对象里面各自存在一个$this 分别代表对象$p1、$p2、$p3。
通过上图我们可以看到,$this 就是对象内部代表这个对象的引用,在对象内部和调用本
对象的成员和对象外部调用对象的成员所使用的方式是一样的。
$this->属性$this->name; $this->age; $this->sex;
$this->方法$this->say(); $this->run();
修改一下上面的实例,让每个人都说出自己的名字,性别和年龄:
代码片段
复制代码 代码如下:
class Person{
//下面是人的成员属性
var $name; //人的名字
var $sex; //人的性别
var $age; //人的年龄
//下面是人的成员方法
function say(){//这个人可以说话的方法
echo "我的名字叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."
";
}
function run(){ //这个人可以走路的方法
echo "这个人在走路";
}
}
$p1=new Person(); //创建实例对象$p1
$p2=new Person(); //创建实例对象$p2
$p3=new Person(); //创建实例对象$p3
//下面三行是给$p1对象属性赋值
$p1->name="张三";
$p1->sex="男";
$p1->age=20;
//下面访问$p1对象中的说话方法
$p1->say();
//下面三行是给$p2对象属性赋值
$p2->name="李四";
$p2->sex="女";
$p2->age=30;
//下面访问$p2对象中的说话方法
$p2->say();
//下面三行是给$p3对象属性赋值
$p3->name="王五";
$p3->sex="男";
$p3->age=40;
//下面访问$p3对象中的说话方法
$p3->say();
?>
输出结果
我的名字叫:张三性别:男我的年龄是:20
我的名字叫:李四性别:女我的年龄是:30
我的名字叫:王五性别:男我的年龄是:40
分析一下这个方法:
代码片段
function say(){ //这个人可以说话的方法
echo "我的名字叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."
";
}
在$p1、$p2 和$p3 这三个对象中都有say()这个方法,$this 分别代表这三个对象,调用相应的属性,打印出属性的值,这就是在对象内部访问对象属性的方式,如果相在say()这个方
法里调用run()这个方法也是可以的,在say()这个方法中使用$this->run()的方式来完成调用。

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
