douchen5971 2017-04-11 12:19
浏览 36
已采纳

访问方法生成的PHP类属性

I feel like I'm missing something incredibly stupid here. I am not too familiar with OOP principals, specifically in PHP and am trying to learn. How do I access a class method's property in PHP? I specifically want to get the $AWSresponse property that is returned from this method. The value of $AWSresponse in JSON form is below.

In UploadHandler.php I have the post() method (full UploadHandler.php class is here):

public $AWSresponse;
public function post($print_response = true) {
    if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
        return $this->delete($print_response);
    }
    $upload = isset($_FILES[$this->options['param_name']]) ?
        $_FILES[$this->options['param_name']] : null;
    // Parse the Content-Disposition header, if available:
    $file_name = $this->get_server_var('HTTP_CONTENT_DISPOSITION') ?
        rawurldecode(preg_replace(
            '/(^[^"]+")|("$)/',
            '',
            $this->get_server_var('HTTP_CONTENT_DISPOSITION')
        )) : null;
    // Parse the Content-Range header, which has the following form:
    // Content-Range: bytes 0-524287/2000000
    $content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
        preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
    $size =  $content_range ? $content_range[3] : null;
    $files = array();
    if ($upload && is_array($upload['tmp_name'])) {
        // param_name is an array identifier like "files[]",
        // $_FILES is a multi-dimensional array:
        foreach ($upload['tmp_name'] as $index => $value) {
            $files[] = $this->handle_file_upload(
                $upload['tmp_name'][$index],
                $file_name ? $file_name : $upload['name'][$index],
                $size ? $size : $upload['size'][$index],
                $upload['type'][$index],
                $upload['error'][$index],
                $index,
                $content_range
            );
        }
    } else {
        if( isset($_POST['fileSourceChooser']) && $_POST['fileSourceChooser']=='dropbox' ){

            //http://justinvincent.com/page/1087/how-to-get-the-mime-type-of-a-remote-file-in-php-with-redirects
            function get_url_mime_type($url){
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_NOBODY, 1);
                curl_exec($ch);
                return curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
            }
                $upload=$_POST['files']; ////if just a regular post
                $upload['parse_url']=parse_url($upload['link']);
                $upload['url']='https://dl.dropboxusercontent.com'.$upload['parse_url']['path'];
                    $files[] = $this->handle_file_upload(
                        $upload['url'],
                        $file_name ? $file_name : $upload['name'],
                        $size ? $size : $upload['bytes'],
                        get_url_mime_type($upload['url']), 
                        "", 
                        null, null,
                        $content_range
                    );  

                    file_put_contents(
                        $this->options['upload_dir'].'thumbnail/'.$upload['name'],
                        fopen($upload['thumbnail'], 'r'),
                        FILE_APPEND //$append_file ? FILE_APPEND : 0
                    );                        
        }
        else{
            // param_name is a single object identifier like "file",
            // $_FILES is a one-dimensional array:
            $files[] = $this->handle_file_upload(
                isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
                $file_name ? $file_name : (isset($upload['name']) ?
                        $upload['name'] : null),
                $size ? $size : (isset($upload['size']) ?
                        $upload['size'] : $this->get_server_var('CONTENT_LENGTH')),
                isset($upload['type']) ?
                        $upload['type'] : $this->get_server_var('CONTENT_TYPE'),
                isset($upload['error']) ? $upload['error'] : null,
                null,
                $content_range
            );                
        }
    }
        $AWSresponse = $this->generate_response(
        array($this->options['param_name'] => $files),
        $print_response
    );
    return $AWSresponse;
}

In index.php I have:

require('UploadHandler.php');
$upload_handler = new UploadHandler();
$r = $upload_handler->AWSresponse;
var_dump($r); // returns NULL

This class (and associated jquery plugin) dumps the following to the browser's console from $AWSresponse, however:

{"files":[{"name":"Test(7).mov","size":1202952,"type":"video\/quicktime","url":"https:\/\/prefix.s3.amazonaws.com\/bucket\/Test%20%287%29.mov","deleteUrl":"https\/\/mysite.com\/server\/php\/?file=Test%20%287%29.mov&_method=DELETE","deleteType":"POST"}]}
  • 写回答

2条回答 默认 最新

  • dongmaoxi0477 2017-04-11 12:59
    关注

    I think you should try this approach:

    require('UploadHandler.php');
    $upload_handler = new UploadHandler();
    $myAWSresponse = $upload_handler->post();
    var_dump($myAWSresponse); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值