douan8473 2018-06-13 02:42 采纳率: 100%
浏览 125
已采纳

PHP基于父类的现有实例创建子类的新实例

I know this is going to be a bit of a weird one, so I'll try to be as brief and as clear as possible.

I am working on a plugin for Grav. I have determined that the most effective way to accomplish my goal is to extend one of the base classes, Pages.

The structure looks something like this:

  • index.php creates an instance of grav class.
  • Base grav class creates an instance of Pages class and stores it as a service.
  • After grav class has finished it's loading process, index.php tells grav to run process on it's service handlers.
  • PagesProcessor runs the init() function on the stored Pages class.
  • PagesProcessor fires an event signaling that pages have been initalized.
  • Plugin receives event.

What this looks like in practical terms.

  1. index.php

    $grav = Grav::instance(
        array(
            'loader' => $loader
        )
    );
    
  2. Grav.php

    protected static $diMap = [
        .....
        'pages' => 'Grav\Common\Page\Pages',
        'pagesProcessor' => 'Grav\Common\Processors\PagesProcessor',
        .....
    ];
    
    protected $processors = [
        .....
        'pagesProcessor',
        .....
    ];
    
    public static function instance(array $values = [])
    {
        if (!self::$instance) {
            self::$instance = static::load($values);
        .....
        return self::$instance;
    }
    
    protected static function load(array $values)
    {
        $container = new static($values);
        .....
        $container->registerServices($container);
    
        return $container;
    }
    
    protected function registerServices()
    {
        foreach (self::$diMap as $serviceKey => $serviceClass) {
            .....
            $this->registerService($serviceKey, $serviceClass);
            .....
        }
    }
    
    protected function registerService($serviceKey, $serviceClass)
    {
        $this[$serviceKey] = function ($c) use ($serviceClass) {
            return new $serviceClass($c);
        };
    }
    
  3. index.php

    try {
        $grav->process();
    } 
    
  4. Grav.php

    public function process()
    {
        foreach ($this->processors as $processor) {
            $processor = $this[$processor];
            .....
                $processor->process();
            .....
        }
        .....
    }
    
  5. PageProcessor.php

    public function __construct(Grav $container)
    {
        $this->container = $container;
    }
    public function process()
    {
        .....
        $this->container['pages']->init();
        $this->container->fireEvent('onPagesInitialized', new Event(['pages' => $this->container['pages']]));
        .....
    }
    
  6. Finally, Plugin.php

    public static function getSubscribedEvents()
    {
        return [
            'onPagesInitialized' => ['onPagesInitialized', 0]
        ];
    }
    
    public function onPagesInitialized(Event $event) {
        // Do things here...
    }
    

Whew... ok.

Now what I would like to do is extend Pages.php (not listed above) so that I can modify some of it's protected properties that don't have setters nativly. The problem is, there is no way to tell grav to use the extended method until AFTER it's been initailized.

I could always create a new instance of my extended class, initailize it, and overwrite the existing instance, but that means effectivly forcing the initailization twice, which means doubling the processing time, and that's super inefficent.

What I would like to do is something like the following:

Pages.php <- I can't change this:

class Pages
{
    protected $grav;

    protected $instances;

    protected $children;

    protected $routes = [];

    public function __construct(Grav $c)
    {
        $this->grav = $c;
    }
    .....
    public function init()
    {
        .....
        $this->instances = [];
        $this->children = [];
        $this->routes = [];

        $this->buildPages();
    }
    .....

PagesExtended.php <- My class

class PagesExtended extends Pages
{
    public function __construct(Pages $original)
    {
        // set the properties of this class to the original instance
        parent = $original;
    }

    public function setInstances(Array $newInstances){
        $this->instances = $newInstances;
    }
    .....
}

Then in my Plugin.php

public function onPagesInitialized(Event $event) {
    // Do things here...
    $this->grav['pages'] = new PagesExtended($event['pages']);
}

The only other thing I can think of is some sort of __call trick.

So.... Thoughts?

EDIT------------------------

Well... I still would like to know if this can be done, but as it turns out grav "freezes" these services so they can't be overridden..... not very happy right now.

  • 写回答

2条回答 默认 最新

  • douzou7012 2018-06-22 23:50
    关注

    It turns out what I was trying to do was impossible in the grav environment, no matter what classes I tried to extend. It could never be done from a plugin because those classes were "frozen" by grav.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值