标签: spl

我应该在PHP中使用哪种Iterator实现,为什么?

我正在尝试重构一个大型的旧项目,我注意到的一件事是一系列不同的Iterator实现:

while($iterator->moveNext()) {
    $item = $iterator->current();
    // do something with $item;
}   

for($iterator = getIterator(), $iterator->HasNext()) {
    $item = $iterator->Next();
    // do something with $item
}   

while($item = $iterator->fetch()) {
    // do something with item
}   
Run Code Online (Sandbox Code Playgroud)

甚至允许的StandardPHPLibrary(SPL)迭代器

foreach($iterator as $item) {
    // do something with $item
}   
Run Code Online (Sandbox Code Playgroud)

有这么多不同的迭代器(使用不同的方法来循环集合)看起来像一个强大的代码味道,我倾向于将所有内容重构为SPL.这些迭代器的实现是否具有令人信服的优势,还是仅仅是个人品味的问题?

php spl iterator design-patterns

4
推荐指数
1
解决办法
478
查看次数

PHP如何在arrayObject上使用array_unshift

正如标题所说,你如何执行array_unshift()arrayObject,array_push()由做一个获得arrayObject->append()但对于不印字?

编辑:我忘了提到的是,在这种特殊情况下我还需要保留现有密钥.

php spl arrayobject

4
推荐指数
3
解决办法
3888
查看次数

PHP 在子文件夹中使用 spl_autoload_register 自动加载

我的目录结构如下所示

> Root
> -Admin // admin area
> --index.php // admin landing page, it includes ../config.php
> -classes // all classes
> ---template.php 
> ---template_vars.php // this file is used inside template.php as $template_vars = new tamplate_vars();
> -templates // all templates in different folder
> --template1
> -index.php
> -config.php
Run Code Online (Sandbox Code Playgroud)

在我使用的 config.php 文件中

<?php
.... // some other php code
spl_autoload_register(NULL, FALSE);
spl_autoload_extensions('.php');
spl_autoload_register();

classes\template::setTemplate('template/template1');
classes\template::setMaster('master');
 .... // some other php code
?>
Run Code Online (Sandbox Code Playgroud)

我已经设置了正确的名称空间(仅在类中),并且在 root 上的 index.php 中我访问了这些类 …

php spl namespaces

4
推荐指数
1
解决办法
2612
查看次数

在迭代它时,ArrayObject不允许我取消设置值

我收到了这个通知:

ArrayIterator :: next():数组在对象外被修改,内部位置在/ var/www中不再有效...

这是由foreach循环开始时由此代码生成的.与通知一起,foreach循环开始重复迭代.换句话说,只要发生这种情况,就会重置内部位置.但根据php手册,ArrayObject默认使用ArrayIterator.

手册说这是关于ArrayIterator的

这个迭代器允许在迭代Arrays和Objects时取消设置和修改值和键.

我在这里错过了什么吗?我发现了一些关于ArratIterator的bug报告,但不是这种.这是一个错误还是我的坏?

版本:PHP版本5.3.10-1ubuntu3.4

<?php
//file 1:
// no namespace
abstract class holder extends \ArrayObject{
    // abstract function init();

    public function __construct($init){
        parent::__construct($init, 1);
    }
}?>

<?php
//file 2:
namespace troops;
class holder extends \holder{
    public function __construct(){
        parent::__construct($this->init());
    }

    private function init(){
        return array( /*... some data from db ...*/ );
    }

    public function saveData(){
        foreach($this as $k  => $v){
            $this->save($v);
            if($v->number_of_items==0) {
                unset($k);
                // $this->offsetUnset($k); // tryed both 
            }
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

php spl arrayobject

4
推荐指数
1
解决办法
3007
查看次数

PHP:我如何排序和过滤"数组",即一个Object,实现ArrayAccess?

我有一个对象集合,表现得像一个数组.它是一个数据库结果对象.类似于以下内容:

$users = User::get();
foreach ($users as $user)
    echo $user->name . "\n";
Run Code Online (Sandbox Code Playgroud)

$users变量是一个实现对象ArrayAccessCountable接口.

我想对这个"数组"进行排序和过滤,但我不能在其上使用数组函数:

$users = User::get();
$users = array_filter($users, function($user) {return $user->source == "Twitter";});
=> Warning: array_filter() expects parameter 1 to be array, object given
Run Code Online (Sandbox Code Playgroud)

如何我可以排序和筛选这种类型的对象?

php arrays spl countable arrayaccess

4
推荐指数
1
解决办法
3559
查看次数

通过Reflection访问SplObjectStorage数据

是否可以访问SplObjectStorage使用Reflection或其他方法的数据?当我使用print_r它时,我可以看到有一个$storage包含所有数据的数组的私有属性,但我无法以任何方式使用Reflection访问它.是否有其他可能的解决方案来获取数据而无需迭代集合foreach

php reflection spl

4
推荐指数
1
解决办法
162
查看次数

用于SimpleXML对象的PHP array_walk_recursive()?

我想将一个函数应用于SimpleXML对象中的每个节点.

<api>
   <stuff>ABC</stuff>
   <things>
      <thing>DEF</thing>
      <thing>GHI</thing>
      <thing>JKL</thing>
   </things>
</api>
Run Code Online (Sandbox Code Playgroud)

// function reverseText($ str){};

<api>
   <stuff>CBA</stuff>
   <things>
      <thing>FED</thing>
      <thing>IHG</thing>
      <thing>LKJ</thing>
   </things>
</api>
Run Code Online (Sandbox Code Playgroud)

如何将reverseText()应用于每个节点以获取第二个XML片段?

php spl simplexml

4
推荐指数
1
解决办法
2214
查看次数

SplPriorityQueue :: next()正在删除该项目

我想知道如何不止一次迭代SplHeap或SplPriorityQueue.

next()方法删除最后一项,因此第二个foreach(或for)没有得到任何结果.

例:

<?php
class Teste extends SplPriorityQueue {}

$teste = new Teste();
$teste->insert( 'A', 1 );
$teste->insert( 'B', 3 );
$teste->insert( 'D', 5 );
$teste->insert( 'C', 2 );
$teste->insert( 'A', 4 );

echo '<pre>';

var_dump( $teste->count() );

echo '<br>';

foreach( $teste as $t )
    var_dump( $t );

echo '<br>';

var_dump( $teste->count() );
Run Code Online (Sandbox Code Playgroud)

返回:

int(5)

string(1) "D"
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "A"

int(0) <--- I need this to still be 5
Run Code Online (Sandbox Code Playgroud)

我需要基于compare()方法插入项目,如下所示:http://php.net/manual/en/splheap.compare.php

谢谢!

php spl iterator next

4
推荐指数
1
解决办法
667
查看次数

从源代码编译的PHP 7.1上安装SPL_Types

我正在尝试从源代码编译的PHP 7.1.8中安装SPL_Types扩展。

我已经尝试过sudo pecl install SPL_Types并从源代码编译扩展,但是得到以下输出:

https://mega.nz/#!WE5WjajQ!QyVxMYWrsUiDF6Gq09psYBpR5Y336v26PusnlBNd8bg

我知道发布链接不是很酷,但是我无法在此处放置孔输出。

php spl php-extension

4
推荐指数
1
解决办法
775
查看次数

PHP SPL库的源代码在哪里?

我可以找到定义文件,http://www.php.net/~helly/php/ext/spl/...但我想扩展DirectoryIteratorSplFileInfo处理存储在数据库而不是真实文件系统上的虚拟文件系统.

php spl

3
推荐指数
1
解决办法
1537
查看次数