如何使用其他函数中的变量?

时间:2018-12-21 12:25:17

标签: php yii

我想使用另一个函数中的另一个变量,

我尝试使用全局变量,但失败了。

class UploadController extends Controller
{
    public $file;// declare my $file varable

    function actionIndex()
    {
        $dir = Yii::getPathOfAlias('application.uploads');

        $uploaded = false;
        $model=new Upload();

        if(isset($_POST['Upload']))
        {
            $model->attributes=$_POST['Upload'];
            global $file; //use the $file varable

            $file=CUploadedFile::getInstance($model,'file');
                 //get a instance into $file varable.

            if($model->validate()){
                $uploaded = $file->saveAs($dir.'/'.$file->getName());
            }
        }
        $this->render('index', array(
                'model' => $model,
                'uploaded' => $uploaded,
                'dir' => $dir,
        ));
    }

    public function actionDownload(){

        var_dump($file);exit; //here printed out a null

        $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."23602414.pdf";

        $upload=new Upload();

        $upload->downloadFile($path);
    }
}

我声明了一个$file变量,并在functin actionIndex中为其分配了一个实例。然后,我想在actionActionDownload函数中使用$file变量,以打印出$file->getname();的返回值

但是,当我“ var_dump($ file); exit;”时,我得到的是空值。在功能actionDownload中。 你能帮我吗?

我试图将代码更改为:

函数actionIndex()     {

        $this->file=CUploadedFile::getInstance($model,'file');

        ...
}

但是,在“ actionDownload()”中。

公共功能actionDownload(){

    var_dump($this);exit;

... }

当我“ var_dump($ this); exit;”时如上所述,我无法获取“文件”的值。打印如下:

E:\projects\yiiroot\trackstar\protected\controllers\UploadController.php:37:
object(UploadController)[10]
  public 'file' => null
  public 'layout' => string '//layouts/column1' (length=17)
  public 'menu' => 
    array (size=0)
      empty
  public 'breadcrumbs' => 
    array (size=0)
      empty
  public 'defaultAction' => string 'index' (length=5)
  private '_id' (CController) => string 'upload' (length=6)
  private '_action' (CController) => 
    object(CInlineAction)[11]
      private '_id' (CAction) => string 'download' (length=8)
      private '_controller' (CAction) => 
        &object(UploadController)[10]
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  private '_pageTitle' (CController) => null
  private '_cachingStack' (CController) => null
  private '_clips' (CController) => null
  private '_dynamicOutput' (CController) => null
  private '_pageStates' (CController) => null
  private '_module' (CController) => null
  private '_widgetStack' (CBaseController) => 
    array (size=0)
      empty
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

你能帮我吗?

2 个答案:

答案 0 :(得分:1)

您不需要将$file声明为全局变量。只需在您的类中声明它,然后您就可以在具有$this关键字的类的任何函数中使用它,因为$this引用您所在的类。就像在actionIndex函数中一样。

 $this->file=CUploadedFile::getInstance($model,'file');

actionDownload()

public function actionDownload(){

    var_dump($this->file);exit; //here printed out a null

    $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."23602414.pdf";

    $upload=new Upload();

    $upload->downloadFile($path);
}

What does the variable $this mean in PHP?

这里是另一个参考文献question

答案 1 :(得分:1)

您已在public $file类中声明了UploadController属性。
那么,为什么不只使用$this关键字呢? 您只需编写以下内容,即可随时随地访问$file属性:

$this->file

例如:

$this->file = CUploadedFile::getInstance($model,'file');