如何使用另一个函数的变量? [英] How to use a variable from another function?

查看:23
本文介绍了如何使用另一个函数的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我声明了一个 $file 变量,并在 functin actionIndex 中为其分配了一个实例.然后,我想在函数 actionDownload 中使用 $file 变量来打印出 $file->getname();

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

但是,当我var_dump($file);exit;"时,我得到了一个空值在功能 actionDownload 中.你能帮我吗?

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:

函数 actionIndex(){

function actionIndex() {

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

        ...
}

但是,在actionDownload()"中.

But, in "actionDownload()".

公共函数 actionDownload(){

public function actionDownload(){

    var_dump($this);exit;

...}

当我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

你能帮我吗.

推荐答案

您已将 public $file 属性声明到您的 UploadController 类中.
那么为什么不直接使用 $this 关键字呢?你可以在任何你想要的地方访问你的 $file 属性:

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

例如:

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

这篇关于如何使用另一个函数的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆