在回答PHP时,rename()不会在出错时抛出异常我想知道是否有任何本机PHP函数抛出内置异常,除了SPL之外?
我正在使用一个SplHeap来保存树的图形节点,该树的节点边缘将从叶子遍历到根.为此,我预先计算节点的"扇入"并将它们放入堆中,这样我就可以始终检索具有最小扇入(0)的节点.
在访问节点之后,我将其后继节点的扇入减少1.显然,需要重新计算堆,因为后续节点现在位于错误的位置.我已经尝试了recoverFromCorruption(),但它没有做任何事情并且保持堆的顺序错误(fanIn在较小的前面有较大停留的节点fanIn).
作为一种解决方法,我现在在每次访问后创建一个新堆,每次都达到完整的O(N*log(N))排序.
但是,应该可以对更改的堆条目进行堆上操作,直到它位于O(log(N))中的正确位置.
API for SplHeap没有提到上堆(或删除任意元素 - 然后可以重新添加).我可以以某种方式派生类SplHeap来执行此操作,还是必须从头开始创建纯PHP堆?
编辑:代码示例:
class VoteGraph {
    private $nodes = array();
    private function calculateFanIn() { /* ... */ }
    // ...
    private function calculateWeights() {
        $this->calculateFanIn();
        $fnodes = new GraphNodeHeap(); // heap by fan-in ascending (leaves are first)
        foreach($this->nodes as $n) {
            // omitted: filter loops
            $fnodes->insert($n);
        }
        // traversal from leaves to root
        while($fnodes->valid()) {
            $node = $fnodes->extract(); // fetch a …Run Code Online (Sandbox Code Playgroud) 在PHP 5.4中,我有一个SplObjectStorage实例,我将对象与一些额外的元数据相关联.然后我需要遍历SplObjectStorage的实例并检索与当前键关联的对象.我试图使用SplObjectStorage :: key,但这不起作用(但可能在PHP 5.5中有效).
这是我试图做的简化版本:
$storage = new SplObjectStorage;
$foo = (object)['foo' => 'bar'];
$storage->attach($foo, ['room' => 'bar'];
foreach ($storage as $value) {
    print_r($value->key());
}
Run Code Online (Sandbox Code Playgroud)
我真正需要的只是某种方法来检索与密钥相关联的实际对象.据我所知,甚至不可能手动创建一个带有数字索引和SplObjectStorage指向的对象的单独索引数组.
我正在使用SplFileInfo和SplFileObject处理PHP中的文件.但当我试图"重新打开"一个文件时,它会让我大叫:
 SplFileObject::__construct(filemame): failed to open stream: Permission denied
Run Code Online (Sandbox Code Playgroud)
我想我应该在重新打开之前关闭我的文件,但我无法确定如何. 
SplFile*没有close功能?!
我正在为lighttpd创建一个列出PHP5脚本的目录.在给定目录中,我希望能够列出直接子目录和文件(带有信息).
快速搜索后,DirectoryIterator似乎是我的朋友:
foreach (new DirectoryIterator('.') as $file)
{
    echo $file->getFilename() . '<br />';
}
Run Code Online (Sandbox Code Playgroud)
但我希望能够按文件名,日期,哑剧类型等对文件进行排序
怎么做(使用ArrayObject/ArrayIterator?)?
谢谢
I'm playing around with the SPL autoload functionality and seem to be missing something important as I am currently unable to get it to work. Here is the snippet I am currently using:
// ROOT_DIRECTORY translates to /home/someuser/public_html/subdomains/test
define('ROOT_DIRECTORY', realpath(dirname(__FILE__)));
define('INCLUDE_DIRECTORY', ROOT_DIRECTORY . '/includes/classes/');
set_include_path(get_include_path() . PATH_SEPARATOR . INCLUDE_DIRECTORY);
spl_autoload_extensions('.class.php, .interface.php, .abstract.php');
spl_autoload_register();
Run Code Online (Sandbox Code Playgroud)
When I echo get_include_path() I do get the path I expected:
// Output echo get_include_path();
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/
Run Code Online (Sandbox Code Playgroud)
However when I run the code I get this error message: …
我有一个表示表单的结构,我想使用RecursiveIterator迭代它.问题是这只会返回顶级问题.我究竟做错了什么?
整体形式:
class Form implements RecursiveIterator{
    private $id;
    private $caption;
    private $other_text;
    private $questions = array();
    private $current;
    private function __construct(DibiRow $row){
        $this->id = $row->id;
        $this->caption = $row->caption;
        $this->other_text = $row->other_text;
        $this->loadQuestions();
    }
    private function loadQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND parent_id IS NULL', $this->id);
        while($question = $questions->fetch()) $this->questions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }
    /**
     * @throws InvalidArgumentException
     * @param $id
     * @return Form
     */
    public static function loadById($id){ …Run Code Online (Sandbox Code Playgroud) 我试着用class:
abstract class my_abstractEnum extends SplEnum {
...
}
Run Code Online (Sandbox Code Playgroud)
和
class my_categoryEnum extends my_abstractEnum {
...
}
Run Code Online (Sandbox Code Playgroud)
我有 :
致命错误:找不到类'SplEnum'
我在PHP 5.2.6上工作.SplEnum用于php> 5.3?我在文档中没有看到......
我正在构建一个框架(这是一个巨大的简化 - 请不要建议使用现有的框架代替,这没有用),我希望能够集成其他库.
PSR-0建议建议每个子命名空间内的所有文件都包含在它们自己的特定目录中.为了使我的框架用户不那么复杂,我想将所有内容保存在一个命名空间中,但是将文件组织到目录中.
如果PHP库可以注册自己的自动加载器spl_register_autoload(),那么为什么必须遵守这个目录结构呢?简单地避开PSR-0,为我的类使用我自己的自动加载器,然后使用(例如)Symfony的自动加载器来处理我可能使用的任何Symfony类是否可行/允许?
有人建议使用SplObjectStorage来跟踪一组独特的东西.很好,除了它不适用于字符串.错误说"SplObjectStorage :: attach()期望参数1是对象,在第59行的fback.php中给出的字符串"
有任何想法吗?