PHP 如何在方法体外部获取内部变量?
过去多啦不再A梦
过去多啦不再A梦 2017-05-19 10:08:55
[PHP讨论组]

1.源码:

    public function importexcel(Request $request){
        $file = Input::file('myfile');
        if($file->isValid()){
            $tmpName = $file->getClientOriginalName();
            $path = $file->move(storage_path().'/uploads',$tmpName);
            $subtmpName = substr($tmpName,0,-5);

            $filePath = "storage/uploads/".iconv('UTF-8', 'UTF-8', $subtmpName).'.xlsx';

            Excel::load($filePath, function($reader) {
                $reader->noHeading();
                $data = $reader->toArray();
                for($i = 1;$i

2.问题:在函数体内变量$result(斜体)如何在方法体外部被调用即代码dd($result)处?

解决:参考:

var1 = 'from func1';
    }
 
    public function func2(){
        echo $this->var1;
    }
}
$demo = new demo();
$demo->func1();
$demo->func2();

在类定义变量,可以解决。有一点不好就是能让类内的所有方法都能访问到。我是希望变量只存在方法importexcel()体内,在 Excel::load($filePath, function($reader) {}变量$return,可以在外Excel::load()被访问到。

过去多啦不再A梦
过去多啦不再A梦

全部回复(2)
PHPz

$result = Excel::load($filePath, function($reader) {

$result = array();
.....
$result[] =  DB::insert('insert into score (id,player_id,node,childnode,time) values (?,?,?,?,?)',[$uuid,$player_id,$node,$childnode,$time]);
}
}
return $result;
PHPz

return $result ,在外部直接使用访问importexcel这个类,由于你这个$result的结果由importexcel函数逻辑生成,不建议直接访问

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号