Home  >  Article  >  Backend Development  >  php中$this->是什么意义

php中$this->是什么意义

WBOY
WBOYOriginal
2016-06-13 11:50:091171browse

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