简体   繁体   中英

PHP - How to pass class to another class method

Im trying to pass the verot image editing class to a custom class that I created, but it doesnt seem to work, it doesnt do anything when I try to run it. How do I pass the verot image class to my class?

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

Here is my custom class. I thought by running upload('') to this method, Im passing a copy of the verot upload class that I can access in my custom class method. But when I run it, it doesnt even get past the $mainimg->uploaded path. In fact $mainimg = $fileupload->upload($savefile); returns NULL when I var_dump it. What am I doing wrong?

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;

            }

        }

    }

It appears this worked, can someone verify this is the proper way of doing this? So instead of this line:

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

This worked.

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

@mr.void Right, so how else would I pass this class to my custom class method? Just seems like this is the wrong way

I think writing a little Factory is the right way for this:

class uploadFac {

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

}

And use it like this:

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

and in the method:

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM