php对象+this,PHP $this:当前对象
在 PHP 面向?qū)ο缶幊讨?#xff0c;對象一旦被創(chuàng)建,在對象中的每個成員方法里面都會存在一個特殊的對象引用“$this”。成員方法屬于哪個對象,“$this”就代表哪個對象,與連接符->聯(lián)合使用,專門用來完成對象內(nèi)部成員之間的訪問。如下所示:
$this -> 成員屬性;
$this -> 成員方法(參數(shù)列表);
比如在 Website 類中有一個 $name 屬性,我們可以在類中使用如下方法來訪問 $name 這個成員屬性:
$this -> name;
需要注意的是,在使用 $this 訪問某個成員屬性時,后面只需要跟屬性的名稱即可,不需要$符號。另外,$this 只能在對象中使用,其它地方不能使用 $this,而且不屬于對象的東西 $this 也調(diào)用不了,可以說沒有對象就沒有 $this。
【示例】使用 $this 調(diào)用類中的屬性和方法。
class Website{
public $name;
public function __construct($name){
$this -> name = $name;
$this -> name();
}
public function name(){
echo $this -> name.'
';
$this -> url();
}
public function url(){
echo 'http://c.biancheng.net/php/
';
$this -> title();
}
public function title(){
echo 'PHP入門教程
';
}
}
$object = new Website('C語言中文網(wǎng)');
?>
運行結(jié)果如下:
C語言中文網(wǎng)
http://c.biancheng.net/php/
PHP入門教程
總結(jié)
以上是生活随笔為你收集整理的php对象+this,PHP $this:当前对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: yum 卸载php及依赖包,yum使用指
- 下一篇: 四川肯德基辣椒包要收费了 部分网友难以