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

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











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
