另一个类的php oop调用方法

时间:2018-10-16 07:59:24

标签: php oop dependencies solid-principles

我是OOP的新手。我正在努力保持SOLID原则。有一些问题。这是我的代码。当我作为依赖项class FileManagerclass GenericUploader或其他方式传递时,是否有一种方法可以访问class Image()中的class Video()方法(使用抽象类,因为我可以提供一些基本功能)在UploaderClient中?目标始终是可以访问class FileManager方法,如果我作为依赖项图像,视频或文件管理器通过,则不会受到限制?

interface IFile
{
    public function crete(array $data);
}

interface IFileManager extends IFile
{
    public function exist(int $id);
}

class FileManager implements IFileManager
{
    public function crete(array $data)
    {
        // store in DB files
    }

    public function exist()
    {

    }
}

class Image implements IFile
{
    public function crete(array $data)
    {
        //access to FileManager
        // store in DB image and files
    }
}

abstract class GenericUploader
{
    public $service;

    public function __construct(IFile $service)
    {
        $this->service = $service;
    }

    public function upload()
    {
        //access to FileManager if $service is new Image()
    }
}

class ImageUploader extends GenericUploader
{
    public function __construct(IFile $service)
    {
        parent::__construct($service);
    }
}

class FileManagerUploader extends GenericUploader
{
    public function __construct(IFile $service)
    {
        parent::__construct($service);
    }
}

class UploaderClient
{
    public function uploadFile(IFile $service)
    {
        $name = get_class($service);
        $uploaderClass = $name . 'Uploader';

        if (class_exists($uploaderClass)) {
            $uploader = new $uploaderClass($service);
            $uploader->upload();
        }
    }
}

$client = new UploaderClient();
$client->uploadFile(new Image());
$client->uploadFile(new FileManager());

更新:例如,如果我扩展Image

class Image extends FileManager implements IFile
    {
        public function crete(array $data)
        {
            //access to FileManager
            // store in DB image and files
        }
    }

从GenericUploader以这种方式($ this-> service-> exist)访问类FileManage方法是否错误?是否违反开放/关闭原则?

1 个答案:

答案 0 :(得分:0)

如果我了解您的问题,请尝试输入我的代码。 trait有一个,它可以为您提供帮助:

UPD

interface IFile
{
    public function crete(array $data);
}

// You can only implement interface, but not extends
class Image implements IFile
{
    public function crete(array $data)
    {
        //access to FileManager
        // store in DB image and files
    }
}

class FileManager implements IFile
{
    public function crete(array $data)
    {
        // store in DB files
    }

    public function exist(int $id)
    {

    }
}

abstract class GenericUploader extends FileManager
{
    public $service;

    public function __construct(IFile $service)
    {
        $this->service = $service;
    }

    public function upload()
    {
        //access to FileManager if $service is new Image()
    }
}

class ImageUploader extends GenericUploader
{
    public function __construct(IFile $service)
    {
        parent::__construct($service);
    }
}

class FileManagerUploader extends GenericUploader
{
    public function __construct(IFile $service)
    {
        parent::__construct($service);
    }
}

class UploaderClient
{
    public function uploadFile(IFile $service)
    {
        $name = get_class($service);
        $uploaderClass = $name . 'Uploader';

        if (class_exists($uploaderClass)) {
            $uploader = new $uploaderClass($service);
            $uploader->upload();
        }
    }
}

$client = new UploaderClient();
$client->uploadFile(new Image());
$client->uploadFile(new FileManager());