dounai1986 2018-03-26 09:33
浏览 109
已采纳

too long

I'm developing a bundle for the community.

My bundle has a service to get some content. I have a folder called Providers with some providers and an AbstractProvider class. All my providers extends the AbstractProvider.

When the developer calls my service's function with a string such as "my_class", I instantiate the class from my providers using this name:

<?php
new MyClass();

In the Providers folder, I have some common providers but I want to allow the developer to add its own providers (extending my AbstractProvider class). My service must instantiate this custom provider using the class name.

How can I achieve this goal?

Here is my tree:

MyBundle
└── Toto
    └── Providers
        ├── AbstractProvider.php
        ├── ArticleProvider.php
        ├── BlockRubricProvider.php
        ├── CommuniqueProvider.php
        ├── HomepageProvider.php
        ├── OffreurSolutionsProvider.php
        ├── PartenaireProvider.php
        ├── PresseProvider.php
        └── PublicationProvider.php

And my service:

class MyServiceProvider
{

    public function __construct() { }

    /* $providerName exemple: "my_provider" */
    public function getProvider( $provierName )
    {
        $providerClass = str_replace('_', '', ucwords($contentType->identifier, '_'))."Provider";

        /** @var AbstractProvider $provider */
        $provider = new $providerClass( $provierName );

        return $provider;
    }
}

How to allow developers to have their own folder Providers with their providers and allow my service to instantiate it with this system ?

Maybe something like this ?

use **\Toto\Providers\*;

Thanks!

  • 写回答

1条回答 默认 最新

  • duanli12176 2018-03-26 10:25
    关注

    Why you don't use the full callified name? In this case you don't need include any use. You can configure the providers with the configuration bundle like here and in your AbstractProvider you can put a abstract method to put a name for the provider that the user use to call it.

    I hope can help you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档