新聞中心
這里有您想知道的互聯網營銷解決方案
PHP關鍵字this指向當前對象指針
PHP關鍵字this是指向當前對象的指針。我們將和大家一起結合一個范例來細細研究一下PHP關鍵字this的相關用法和具體功能體現。#t#

網站設計、成都網站建設服務團隊是一支充滿著熱情的團隊,執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯的標準與要求,同時竭誠為客戶提供服務是我們的理念。成都創(chuàng)新互聯把每個網站當做一個產品來開發(fā),精雕細琢,追求一名工匠心中的細致,我們更用心!
- < ?php
- class UserName
- {
- //定義屬性
- private $name;
- //定義構造函數
- function __construct( $name )
- {
- $this->name = $name;
//這里已經使用了this指針 - }
- //析構函數
- function __destruct(){}
- //打印用戶名成員函數
- function printName()
- {
- print( $this->name );
//又使用了PHP關鍵字this指針 - }
- }
- //實例化對象
- $nameObject = new UserName
( "heiyeluren" ); - //執(zhí)行打印
- $nameObject->printName();
//輸出: heiyeluren - //第二次實例化對象
- $nameObject2 = new UserName( "PHP5" );
- //執(zhí)行打印
- $nameObject2->printName(); //輸出:PHP5
- ?>
我 們看,上面的類分別在11行和20行使用了this指針,那么當時this是指向誰呢?其實this是在實例化的時候來確定指向誰,比如第一次實例化對象 的時候(25行),那么當時this就是指向$nameObject對象,那么執(zhí)行18行的打印的時候就把print( $this->
第二個實例的時候,print( $this->name )變成了print( $nameObject2->name ),于是就輸出了"PHP5"。所以說,PHP關鍵字this就是指向當前對象實例的指針,不指向任何其他對象或類。
名稱欄目:PHP關鍵字this指向當前對象指針
新聞來源:http://www.5511xx.com/article/djpigsh.html


咨詢
建站咨詢
