php面向对象 $this关键字用法详解
本文章来给各位同学介绍在php类中我们会常用到this 关键字了,下面我简单的分析一下在php面向对象 $this关键字用法希望此教程对各位同学会有所帮助呀。
在前面我们知道,在对象外部访问对象成员属性和方法,使用对象的引用来完成。而在对象内部中,成员方法访问自己对象中的其它成员属性或者成员方法,要使用特殊的对象引用"$this->值"的形式来访问,成员属于哪个对象,$this引用就代表哪个对象,并且只能在对象的成员方法中使用。
为了解决php类和对象中变量与属性的命名冲突和不确定性问题,引入了”$this”关键字来调用当前的对象。
在类中调用当前对象的属性和方法,必须使用”$this->”关键字;$this在构造函数中指该构造函数所创建的新对象;方法内的局部变量不属于对象,不使用$this关键字取值。使用$this关键字,我们可以在类中调用对象属性或者方法。
1、调用变量
实例:
代码如下 | 复制代码 |
class user{ private $n; function __construct(){ $name="Mike"; echo $this->n=$name; } } $p=new user(); ?> |
2、调用方法
实例:
代码如下 | 复制代码 |
class cal{ |
我们来看一下下面的例子,$this在做了什么?
代码如下 | 复制代码 |
class Person{ //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值 function __construct($name="", $sex="", $age=""){ $this->name=$name; $this->sex=$sex; $this->age=$age; } //这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age." "; } //对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原本 的属性和方法 function __clone(){ //$this指的复本p2, 而$that是指向原本p1,这样就在本方法里,改变了复本的属性。 $this->name="我是假的$that->name"; $this->age=30; } } $p1=new Person("张三", "男", 20); $p2=clone $p1; $p1->say(); $p2->say(); ?> |
上例输出:
执行结果
我的名子叫:张三性别:男我的年龄是:20
我的名子叫:我是假的张三性别:男我的年龄是:30
我们来看一下上面的代码中$this做了什么:
1、访问对象内部的成员,如$this->name
2、访问对象的其它方法,如在say()方法内部访问了其之外的$this->run()和$this->eat("apple")方法。
另外还有一点值得注意的是,局部变量和成员属性可以同名,但作用范围和访问方式不一样,如在eat()方法体内的$name相当于局部变量,其作用范围只限于eat方法内部,而Person的成员属性声明部分的 $name,则相当于全局变量,可以其它方法中使用$this->name的形式访问。
例
代码如下 | 复制代码 |
class UserName |

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



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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