2017-04-11 126 views
0

我覺得我在這裏失去了一些令人難以置信的愚蠢。我不太熟悉OOP校長,特別是在PHP中,我正在努力學習。如何在PHP中訪問類方法的屬性?我特別想獲取從此方法返回的$ AWSresponse屬性。 JSON形式的$ AWSresponse的值如下。訪問PHP方法生成的方法

在UploadHandler.php我有post()方法(全UploadHandler.php類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; 
} 

在index.php文件,我有:

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

這個類(和關聯的jquery插件)將以下內容從$ AWSresponse轉儲到瀏覽器的控制檯:

{"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"}]} 
+0

vardump應該是var_dump – Nikkorian

+0

哎呀。我已經修復了代碼,但我仍然遇到了同樣的問題。 – thebarless

+0

我真的不明白你的問題是什麼?什麼是輸出_suppose_看起來像什麼? –

回答

1

我想你應該試試這個方法:

require('UploadHandler.php'); 
$upload_handler = new UploadHandler(); 
$myAWSresponse = $upload_handler->post(); 
var_dump($myAWSresponse); 
0

我認爲你應該使用$this->AWSresponse訪問類屬性在function post(),如:

return $this->AWSresponse; 

,當你在$AWSresponse使用$this->AWSresponse像賦值:

$this->AWSresponse = $this->generate_response(
     array($this->options['param_name'] => $files), 
     $print_response 
    ); 
+0

如果我從課外以外打電話,這個$是否可以工作? – thebarless

+0

編號'$ this'是指「當前」對象的實例。 – domsson

+0

好的,這就是我的想法。也許我是在購物車之前在我的腦海中搖擺。這個班級正在做我想做的事情,並將數據輸出到瀏覽器的控制檯。我錯在想我需要修改班級嗎? – thebarless