How to use a variable from another function?

2019-08-16 13:15发布

I want to use another variable from another function,

I am tried to use the global variable, but I faild.

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);
    }
}

I declared a $file varable and assigned an instance into it in the functin actionIndex. Then, I want to use the $file varable at function actionDownload in order to print out the return value of $file->getname();

But, I got a null when I "var_dump($file);exit;" in function actionDownload. Could you help me?

I tried to change my code like:

function actionIndex() {

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

        ...
}

But, in "actionDownload()".

public function actionDownload(){

    var_dump($this);exit;

... }

When I "var_dump($this);exit;" as above, I could't get the value of 'file'. It printed out as below:

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

Could you help me.

标签: php yii
2条回答
该账号已被封号
2楼-- · 2019-08-16 13:27

You have declared a public $file property into your UploadController class.
So why not just use $this keyword? You can access your $file property wherever you want just by writing:

$this->file

For example:

$this->file = CUploadedFile::getInstance($model,'file');
查看更多
我只想做你的唯一
3楼-- · 2019-08-16 13:30

You don't need to declare $file as a global variable. Just declare it in your class and then you can use it in any function of a class with $this keyword as $this refers to the class you are in. Like in actionIndex function.

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

And in 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?

Here is another reference question

查看更多
登录 后发表回答