php:如何在类中的函数外部使用var


php:how to use a var outside a function inside a class

我在类中创建了一个类似的函数

public function admin_ads_manage(){
$ads728=$_POST['ads728'];
$ads600=$_POST['ads600'];
$ads300=$_POST['ads300'];

$file728=stripslashes(file_get_contents($this->dir_name."/ads/ads728.txt"));
$file600=stripslashes(file_get_contents($this->dir_name."/ads/ads600.txt"));
$file300=stripslashes(file_get_contents($this->dir_name."/ads/ads300.txt"));
if($_POST['submit']){
    $f728=fopen($this->dir_name."/ads/ads728.txt",'w');
    $w728=fwrite($f728,$ads728);
    $f600=fopen($this->dir_name."/ads/ads600.txt",'w');
    $w600=fwrite($f600,$ads600);
    $f300=fopen($this->dir_name."/ads/ads300.txt",'w');
    $w300=fwrite($f300,$ads300);
    echo "<meta http-equiv='"refresh'" content='"0'" " ;

    }

我想使用这个函数内部的变量(仅)在它外部打印索引页中的输出,如何访问这个函数以允许只使用一些var,而不是执行整个函数

我知道我可以通过$object->function()来执行整个函数;但我只想使用一些变量……

private $myVar;
public function admin_ads_manage(){
   $this->myVar = 'Your value';
   // Rest of the code
}
public function getMyVar() {
  return $this->myVar;
}
// Where you what to use it
echo $object->getMyVar();

创建公共实例变量并访问它。

class A
{
  public $var;
  public function admin_ads_manage()
  {
      //skip some code
      $this->var = "value to be accessed from outside";
  }
}