search
Homephp教程php手册php中$this-)是什么意思?

$this 的含义表示什么,实例化后的具体对象!

我们一般是先声明一个类,然后用这个类去实例化对象!

但是,当我们在声明这个类的时候,想在类本身内部使用本类的属性或者方法。应该怎么表示呢?

例如:

我们声明一个User类!它只含有一个属性$name;

classname

{

public $_name;

}

现在我们给User类加个方法。使用getName()方法,输出$name属性的值吧!复制php内容到剪贴板

php代码:

class User

{

public $name;

function getName()

{

echo $this->name;

}

}


//如何使用呢?

$user1=new User();

$user1->name='张三';

$user1->getName();

$user2=new User();

$user2->name='李四';

$user2-getName();

怎么理解呢?

我上面创建了两个User对象。分别是$user1和$user2.

当我调用$user1->getName()的时候。上面User类中的代码echo $this->name ;就是相当于是echo $user1->name;

大概就是这个意思!

其实,你也不要去钻牛角尖。你只要知道那是一个用来表示类内部的属性和方法的代号就好了!越想越糊涂的!




Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor