我使用SplObjectStorage实现了一个简单的Composite模式,就像上面的例子一样:
class Node
{
    private $parent = null;
    public function setParent(Composite $parent)
    {
        $this->parent = $parent;
    }
}
class Composite extends Node
{
    private $children;
    public function __construct()
    {
        $this->children = new SplObjectStorage;
    }
    public function add(Node $node)
    {
        $this->children->attach($node);
        $node->setParent($this);
    }
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试序列化一个Composite对象时,PHP 5.3.2就会抛出一个Segmentation Fault.只有在向对象添加任意类型的任意数量的节点时才会发生这种情况.
这是违规的代码:
$node = new Node;
$composite = new Composite;
$composite->add($node);
echo serialize($composite);
Run Code Online (Sandbox Code Playgroud)
虽然这个有效:
$node = new Node;
$composite = new Composite;
echo serialize($composite);
Run Code Online (Sandbox Code Playgroud)
另外,如果我使用array()而不是SplObjectStorage实现Composite模式,那么所有运行也都可以.
我做错了什么?
我正在尝试设计一个PHP对象(称之为Incident_Collection),它将包含其他对象的集合,每个对象都实现一个Incident接口.
<?php
class Foo implements Incident {
  protected $incident_date; //DateTime object
  protected $prop1;
  protected $prop2;
  //etc
  public function when(){ //required by Incident interface
    return $this->incident_date;
  }
}
?>
Run Code Online (Sandbox Code Playgroud)
起初我想我只是创建我的Incident_Collection工具IteratorAggregate并将Incident对象存储在集合的数组属性中:
<?php
class Incident_Collection implements IteratorAggregate {
  protected $collection=array();
  public function getIterator(){
    return new ArrayIterator($this->collection);    
  }
  public function sort(){
     //sort by $incident->when() values in $this->collection
  }
  /*also __get($var), __set($var,$value), add(Incident $object), remove(Incident $object) and other functions*/
}
?>
Run Code Online (Sandbox Code Playgroud)
但是因为Incident对象具有自然顺序,我认为可能扩展其中一个SPL数据结构可能更合适/更有效.但是哪一个? …
好吧,正如标题所说:使用ArrayIterator而不是简单的foreach循环有什么主要好处.
我有一个被用作容器的对象,它的主要职责是存储一个对象数组.
有人建议我让我的类实现IteratorAggregate并返回一个带有我的对象数组的ArrayIterator:像这样:
  public function getIterator()
  {  
    return new ArrayIterator($this->_objs);  
  }
Run Code Online (Sandbox Code Playgroud)
我不能看到任何好处,而不是简单地返回数组,然后使用标准的foreach循环它们.
请有人解释为什么这是个好主意?
我在这个主题上看到了很多问题,但没有一个问题涉及我的案例.
我正在构建一个基于5个类的ACL模块:
我正在考虑使用SplQueue来存储权限级别(主要用于继承的权限)所以我想使用单个对象来存储所有内容,并且不认为普通的多维数组将是最佳选择.流程就像这个粘贴,它是TL; DR ..对不起.
所以我的问题是SplQueue在我的情况下是否有点过分?
我应该使用和替代Spl数据结构,如果是这样的话?
编辑 好吧我想不出一个很好的用法示例,所以让我们继续使用基于UNIX的GBAC.
有没有办法对SplFixedArray类的实例中的整数或字符串执行排序?转换为PHP array,排序,然后转换回来是唯一的选择?
为什么会UnexpectedValueException被抛入session_start()?
我有具有属性的对象SPLObjectstorage.该对象被分配给会话
$_SESSION['foo'] = $barObject;
Run Code Online (Sandbox Code Playgroud)
我怀疑内部会话序列化面临问题解码它.我将会话存储在数据库中,看起来它正在序列化objectStorage但无法对其进行解码.
会话数据示例
self|O:4:"User":8:{s:5:"?*?id";N;s:7:"?*?nick";N;s:13:"?*?reputation";i:1;s:11:"?*?password";N;s:8:"?*?email";N;s:7:"?*?crud";O:10:"CRUDobject":2:{s:13:"?*?fieldCache";a:0:{}s:13:"?*?dependency";r:1;}s:7:"?*?auth";N;s:11:"?*?roleList";C:11:"RoleStorage":23:{x:i:1;N;,r:13;;m:a:0:{}}}
Run Code Online (Sandbox Code Playgroud)
RolestorageSPLObjectstorage
session_decode()在上面的字符串的延伸也返回false任何想法?
删除roleList属性使其正确序列化.
如果我单独做
$sr = serialize($roles); // $roles is RoleStorage object
var_dump($sr);
var_dump(unserialize($sr));
Run Code Online (Sandbox Code Playgroud)
它string 'C:11:"RoleStorage":22:{x:i:1;N;,r:3;;m:a:0:{}}' (length=46)在反序列化时打印然后失败并显示相同的消息.我不知道为什么会这样.
注意:在将对象附加到RoleStorageI时,将对象本身用作数据.表示它存储为参考.我不知道(if)如何在serialize()内部处理这个问题.
RecursiveDirectoryIterator 似乎从我的localhost和live服务器给我两个不同的结果,
define ('WEBSITE_DOCROOT', str_replace('\\', '/', dirname(__FILE__)).'/');
print_r(WEBSITE_DOCROOT);
// List all the class directories in the array.
$main_directories = array(
    'core/model/',
    'core/helper/',
    'core/ext/'
);
// Set other vars and arrays.
$sub_directories = array();
// List any sub dirs in the main dirs above and store them in an array.
foreach($main_directories as $path_directory)
{
    $iterator = new RecursiveIteratorIterator
    (
        new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory), // Must use absolute path to get the files when ajax is used.
        RecursiveIteratorIterator::SELF_FIRST
    );
    foreach ($iterator as $fileObject) 
    { …Run Code Online (Sandbox Code Playgroud) 我使用laravel 4(最新更新),我创建了一个可以上传图像(徽标/头像)的表单。我在MAC OS上,我使用sublime Text 3,laravel和MAMP Applications。我所有的配置都正确,没有运行问题。
我的问题是,当我从表单提交到达字段时遇到此错误:RuntimeException SplFileInfo :: getSize():/ Applications / MAMP / tmp / php / phpRUJfMX的统计信息失败
这是我的表单nameOfTheForm.blade.php中的代码:
@extends('layout.default')
@section('title')
Name Of My Project - EditProfile
@stop
@section('content')
{{Form::open(array('url'=>'uploadAvatar','files' => true))}}
<p>
{{Form::label('pseudo','pseudo (*): ')}}
{{Form::text('pseudo',Input::old('nom'))}}
</p>
@if ($errors->has('pseudo'))
<p class='error'> {{ $errors->first('pseudo')}}</p>
@endif
<br>
<br>
<p>
{{Form::label('url_Avatar','Avatar: ')}}
{{Form::file('url_Avatar',Input::old('Url_Avatar'))}}
</p>
@if ($errors->has('url_Avatar'))
<p class='error'> {{ $errors->first('url_Avatar')}}</p>
@endif
<br> …Run Code Online (Sandbox Code Playgroud) 我使用RecursiveIteratorIterator迭代一个多维数组,并希望能够知道当前元素是否是它深度的最后一个子元素.我想到了这个:
$iterator = new RecursiveIteratorIterator($array,
  RecursiveIteratorIterator::SELF_FIRST);    
foreach ($iterator as $val) {
  $next = clone $iterator;
  $next->next();
  $lastChild = ($next->getDepth() < $iterator->getDepth());
}
Run Code Online (Sandbox Code Playgroud)
但RecursiveIteratorIterator表示它不可克隆.
当与SplFixedArray一起使用时,我看到了一些奇怪的行为($ arr,COUNT_RECURSIVE).以这段代码为例,......
$structure = new SplFixedArray( 10 );
for( $r = 0; $r < 10; $r++ )
{
    $structure[ $r ] = new SplFixedArray( 10 );
    for( $c = 0; $c < 10; $c++ )
    {
        $structure[ $r ][ $c ] = true;
    }
}
echo count( $structure, COUNT_RECURSIVE );
Run Code Online (Sandbox Code Playgroud)
结果...
> 10
Run Code Online (Sandbox Code Playgroud)
你会期望110的结果.这是正常的行为,因为我正在嵌套SplFixedArray对象吗?