Php 如何使用另一个函数中的变量?;

Php 如何使用另一个函数中的变量?;,php,yii,Php,Yii,我想使用另一个函数中的另一个变量 我试图使用全局变量,但失败了 class UploadController extends Controller { public $file;// declare my $file varable function actionIndex() { $dir = Yii::getPathOfAlias('application.uploads'); $uploaded = false;

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

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

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
变量,并在
函数actionIndex
中为其分配了一个实例。然后,我想在actionDownload函数中使用
$file
变量来打印
$file->getname()的返回值

但是,当我在actionDownload函数中“var_dump($file);exit;”时,得到一个空值。 你能帮我吗

我试图更改代码,如下所示:

函数actionIndex() {

但是,在“actionDownload()”中

公共函数actionDownload(){

。。。 }

当我像上面那样“var_dump($this);exit;”时,我无法获取“file”的值。它打印出来如下:

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

你能帮助我吗。

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

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


这里是另一个参考

您已经在
UploadController
类中声明了
public$file
属性。
那么为什么不直接使用
$this
关键字呢? 只要写下:
就可以在任何地方访问
$file
属性

$this->file
例如:

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

为什么不使用
$this
关键字?我根据下面的提示解决了这个问题。谢谢你,我按照你说的重写了我的代码,但是在var_dump($this->file);exit;我得到一个错误:“Undefined variable:file”。你能告诉我为什么吗?好的,那么你可以在
actionIndex
函数中返回值,然后在
actionDownload
函数中作为参数传递使用该值谢谢,但是当我的前端调用“r=upload/download”时,我必须将参数传递到“public function actionDownload()”也是?因为前端将直接调用“actionDownload()”,我不知道如何将参数从actionIndex()传递到actionDownload()。您能告诉我更详细的信息吗?您必须同时调用这两个函数。首先,将
actionDownload()
分配给一个变量,然后将其传递到
actionDownload()
这对你来说是一个简单的方法,我试图使用“$this->file”,但当我“var_dump($this->file);exit;”我得到一个空值,$this->file=cuploatedfile::getInstance($model,'file');$file=$this->file;/*var_dump($file);exit;*/if($model->validate()){$upload=$file->saveAs($dir./'.$file->getName())}$this->render->('index',array('model'=>$model,'upload'=>$upload,'dir'=>$dir,);}公共函数actionDownload(){var_dump($this->file);exit;$path=Yii::getPathFalias('/yiiroot/trackstar/protected/uploads/')。'23602414.pdf;$upload=new upload();$upload->downloadFile($path);}}你能告诉我为什么我得到空值吗?在公共函数actionDownload()中,{var_dump($this->file);exit;…}谢谢。因为你的
$file
属性是空的。首先,你需要使用
函数actionIndex()
写一些东西给它,我认为最好声明
actionIndex()
作为
public
方法,因为你做了一件丑陋的事情。首先,你应该调用
public函数actionIndex()
来填充你的$file属性,你可以通过
$this->file
actionDownload()
方法.GLHF!3中使用它
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);
}
$this->file
$this->file = CUploadedFile::getInstance($model,'file');