Sla*_*uip 6 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.
Run Code Online (Sandbox Code Playgroud)
我做了一些谷歌,但找不到任何东西,我猜这是不可能的,因为它打破了某个地方的OOP校长,但我想我还是要问!
我认为我们可以将其分解为以下几个问题:
Collection
可迭代的应该Collection
A。具有硬编码(坏)的禁止方法的名称或
b. 能够从集合的元素中获取禁止方法的名称
当迭代集合时,它应该产生原始对象的代理,拦截对在迭代集合时不允许调用的方法的调用
这很简单,只需让它实现Iterator
接口即可:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我建议不要将禁止的方法硬编码到集合中,而是建议创建一个接口,并在需要时由集合的元素实现该接口,例如:
<?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();
}
Run Code Online (Sandbox Code Playgroud)
这还有一个优点,即集合可以处理所有类型的元素,只要它能够从元素中获取该信息即可。
然后让您的元素(如果需要)实现该接口:
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',
];
}
}
Run Code Online (Sandbox Code Playgroud)
正如 @akond 在另一个答案中所建议的,您可以使用ocramius/proxymanager
,特别是Access Interceptor Value Holder Proxy。
跑步
$ composer require ocramius/proxymanager
Run Code Online (Sandbox Code Playgroud)
将其添加到您的项目中。
调整集合如下:
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
<?php
require_once __DIR__ .'/vendor/autoload.php';
$elements = [
new Element(),
new Element(),
new Element(),
];
$collection = new Collection($elements);
foreach ($collection as $element) {
$element->foo();
}
Run Code Online (Sandbox Code Playgroud)
注意这仍然可以进行优化,例如,您可以在 中存储对已创建代理的引用Collection
,并且current()
如果需要,可以返回以前创建的代理,而不是每次都创建新代理。
作为参考,请参阅: