簡體   English   中英

如何在類中但在函數外部訪問變量?

[英]how to access variables in a class but outside a function?

我做了以下代碼:

<?php
 class hoi {
  public $a = 1;
   function test()
   { 
    echo $this->$a; /* reference to alocal scope variable? */ 
   } 
  }
 $hoi = new hoi;
 $hoi->test();
?>

我嘗試回顯$ a,但是這不起作用,我如何回顯在類內部但在函數外部聲明的變量?

語法為:

$this->a

在屬性中使用一個附加的$變量。

class hoi {
    public $a = 1;
    function test() { 
        echo $this->a;/* the variable is accessed like this - no need for the $ */
    } 
}
$hoi = new hoi();/* required as there is no __construct() method */
$hoi->test();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM