PHP - 如何将类传递给另一个类方法

时间:2017-04-11 17:34:03

标签: php class verot-upload-class

我试图将verot图像编辑类传递给我创建的自定义类,但它似乎不起作用,当我尝试运行它时它什么都不做。如何将verot图像类传递给我的班级?

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

这是我的自定义类。我想通过运行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;

            }

        }

    }

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);