PHP類與對象中的$this使用方法


概念:

$this:可以讀取到類里面的屬性,還可以讀取到方法;

不管生成的對象名稱是什么,$this只指向當前對象。

<?php 


Class User{
  protected $name;

public function getName($name){

  return $this-> name = $name;  //$this獲取類的屬性

}

public function sayName(){

 return $this->name;

}

public function funName(){

  return $this -> sayName();  //$this獲取類的方法

}

}

$Obj = new User;  //生成對象
$Obj->getName("lilei");
echo $Obj -> funName();


?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM