php - 当作为集合的一部分对其进行迭代时,对象的方法是否可以被拦截?

标签 php oop iterator proxy-object

我想知道一个属于集合类的对象在被迭代时是否可以知道它正在被迭代并知道它所属的集合类?例如

<?php
class ExampleObject
{
    public function myMethod()
    {
        if( functionForIterationCheck() ) {
           throw new Exception('Please do not call myMethod during iteration in ' . functionToGetIteratorClass());
        }
    }
}

$collection = new CollectionClass([
    new ExampleObject,
    new ExampleObject,
    new ExampleObject
]);

foreach($collection as $item) {
    $item->myMethod(); //Exception should be thrown.
}

(new ExampleObject)->myMethod(); //No Exception thrown.

我用 Google 搜索了一下,但找不到任何东西,我猜这是不可能的,因为它在某处破坏了 OOP 原则,但我还是想问一下!

最佳答案

我认为我们可以将其分解为以下问题:

  1. 我们需要创建一个可迭代的Collection
  2. Collection 应该

    一个。将禁止方法的名称硬编码(错误)

    能够从集合的元素中获取禁止方法的名称

  3. 在遍历集合时,它应该生成原始对象的代理,拦截对在遍历集合时不应被调用的方法的调用

1) 集合应该是可迭代的

这很简单,只需让它实现Iterator接口(interface)即可:

class Collection implements \Iterator
{
    /**
     * @var array
     */
    private $elements;

    /**
     * @var int
     */
    private $key;

    public function __construct(array $elements)
    {
        // use array_values() here to normalize keys 
        $this->elements = array_values($elements);
        $this->key = 0;
    }

    public function current()
    {
        return $this->elements[$this->key];
    }

    public function next()
    {
        ++$this->key;
    }

    public function key()
    {
        return $this->key;
    }

    public function valid()
    {
        return array_key_exists(
            $this->key,
            $this->elements
        );
    }

    public function rewind()
    {
        $this->key = 0;
    }
}

2) 集合应该能够从元素中获取方法

与其将禁止的方法硬编码到集合中,我建议创建一个接口(interface),并在需要时由集合的元素实现,例如:

<?php

interface HasProhibitedMethods
{
    /**
     * Returns an array of method names which are prohibited 
     * to be called when implementing class is element of a collection.
     *
     * @return string[]
     */
    public function prohibitedMethods();
}

这还有一个好处,即集合可以处理所有类型的元素,只要它能够从元素中获取信息即可。

然后让你的元素,如果需要的话,实现接口(interface):

class Element implements HasProhibitedMethods
{ 
    public function foo()
    {
        return 'foo';
    }

    public function bar()
    {
        return 'bar';
    }

    public function baz()
    {
        return 'baz';
    }

    public function prohibitedMethods()
    {
        return [
            'foo',
            'bar',
        ];
    }
}

3) 迭代时,产生代理

正如@akond 在另一个答案中所建议的那样,您可以使用 ocramius/proxymanager ,具体来说,一个 Access Interceptor Value Holder Proxy .

运行

$ composer require ocramius/proxymanager

将其添加到您的项目中。

调整集合如下:

<?php

use ProxyManager\Factory\AccessInterceptorValueHolderFactory;

class Collection implements \Iterator
{
    /**
     * @var array
     */
    private $elements;

    /**
     * @var int
     */
    private $key;

    /**
     * @var AccessInterceptorValueHolderFactory
     */
    private $proxyFactory;

    public function __construct(array $elements)
    {
        $this->elements = array_values($elements);
        $this->key = 0;
        $this->proxyFactory = new AccessInterceptorValueHolderFactory();
    }

    public function current()
    {
        $element = $this->elements[$key];

        // if the element is not an object that implements the desired interface
        // just return it
        if (!$element instanceof HasProhibitedMethods) {
            return $element;
        }

        // fetch methods which are prohibited and should be intercepted
        $prohibitedMethods = $element->prohibitedMethods();

        // prepare the configuration for the factory, a map of method names 
        // and closures that should be invoked before the actual method will be called
        $configuration = array_combine(
            $prohibitedMethods,
            array_map(function ($prohibitedMethod) {
                // return a closure which, when invoked, throws an exception
                return function () use ($prohibitedMethod) {
                    throw new \RuntimeException(sprintf(
                        'Method "%s" can not be called during iteration',
                        $prohibitedMethod
                    ));
                };
            }, $prohibitedMethods)
        );

        return $this->proxyFactory->createProxy(
            $element,
            $configuration
        );
    }

    public function next()
    {
        ++$this->key;
    }

    public function key()
    {
        return $this->key;
    }

    public function valid()
    {
        return array_key_exists(
            $this->key,
            $this->elements
        );
    }

    public function rewind()
    {
        $this->key = 0;
    }
}

例子

<?php

require_once __DIR__ .'/vendor/autoload.php';

$elements = [
    new Element(),
    new Element(),
    new Element(),
];

$collection = new Collection($elements);

foreach ($collection as $element) {
    $element->foo();
}

注意 这仍然可以优化,例如,您可以将对创建的代理的引用存储在 Collection 中,而不是每次都创建新的代理,current() 可以返回以前创建的代理,如果需要的话。

引用:

关于php - 当作为集合的一部分对其进行迭代时,对象的方法是否可以被拦截?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45669043/

相关文章:

php - file_get_contents 不在本地主机上工作,但在在线网络服务器上工作

Python __init__/General Function Inline Equals In Argument(s) :

c++ - c++ 按降序对 vector 进行排序

php - 左对齐和右对齐CSS中的一个元素内容

php - PHP SOAP 调用中的多个同名元素

javascript - 在javascript中声明 protected 变量

php - OOP方式的蛋鸡问题

iterator - Rust 实现合并排序迭代器

c++ - 有条件地向前或向后迭代的优雅方式

php - 通过代理使用 PHPMailer 发送邮件?