2017-04-11 92 views
0

我試圖將verot圖像編輯類傳遞給我創建的自定義類,但它似乎無效,它在我嘗試運行時沒有做任何事情。我如何將維羅圖片類傳遞給我的班級?PHP - 如何將類傳遞給另一個類的方法

//Edit.php 
//Now I run my class 
$process = new ProcessEventLogo(); 
$process->editEventLogo($event_id,$file_ext,$savepath,new upload('')); 

這是我的自定義類。我想通過運行上傳('')這個方法,我傳遞了一個我可以在我的自定義類方法中訪問的verot上傳類的副本。但是當我運行它時,它甚至不會超過$ mainimg->上傳的路徑。其實$ mainimg = $ fileupload-> upload($ savefile);當我var_dump時返回NULL。我究竟做錯了什麼?

class ProcessEventLogo { 

    public function editEventLogo($eventid,$fext,$url,$savepath,$fileupload) 
    { 

     //We generate the file name to save this image to 
     $savefile = $savepath .'event_' .$eventid .'.' .$fext; 

     //We check to see if the event image is there 
     $mainimg = $fileupload->upload($savefile); 

     //We now resize the image 
     if($mainimg->uploaded) 
     { 

      $mainimg->file_overwrite = TRUE; 
      $mainimg->image_ratio_crop = TRUE; 
      $mainimg->image_resize = TRUE; 
      $mainimg->image_x = 50; 
      $mainimg->image_y = 50; 
      $mainimg->process($savefile); 

      if($mainimg->processed) 
      { 
       echo $mainimg->error; 

      } 

     } 

    } 

回答

0

看來這工作,有人可以驗證這是這樣做的正確方法?所以,而不是這條線:

//We check to see if the event image is there 
$mainimg = $fileupload->upload($savefile); 

這工作。

//We check to see if the event image is there 
$mainimg = new $fileupload($savefile); 
+0

不太確定,也許我錯了,但似乎如果你首先在你的函數中創建一個新的上傳('')「,你創建了一個類的實例,試圖上傳一個沒有找到的文件,在這種情況下''(看看uploadclass的__construct」https://github.com /verot/class.upload.php/blob/master/src/class.upload.php「),並在第二次調用」new $ fileupload($ savefile);「你用一個有效的文件名創建一個新的實例 –

+0

@ mr.void對,那麼我該怎麼把這個類傳遞給我的自定義類方法呢?看起來這是錯誤的方式。 – John

0

@ mr.void對,所以怎麼回事就我這個類傳遞給我的自定義類 方法?似乎只是這是錯誤的方式

我覺得寫一個小廠是這樣做的正確方法:

class uploadFac { 

    public function getUploadInstance($file) 
    { 
     return new upload($file) 
    } 

} 

而且使用這樣的:

$uplFac = new uploadFac(); 
$process = new ProcessEventLogo(); 
$process->editEventLogo($event_id,$file_ext,$savepath,$uplFac); 

,並在方法:

public function editEventLogo($eventid,$fext,$url,$savepath,$uplFac) 
    { 
     //We generate the file name to save this image to 
     $savefile = $savepath .'event_' .$eventid .'.' .$fext; 
     $mainimg = $uplFac->getUploadInstance($savefile);