spl

Using foreach with SplFixedArray

强颜欢笑 提交于 2019-12-04 06:23:49
问题 It seems like I can't iterate by reference over values in an SplFixedArray: $spl = new SplFixedArray(10); foreach ($spl as &$value) { $value = "string"; } var_dump($spl); Outputs: Fatal error: Uncaught exception 'RuntimeException' with message 'An iterator cannot be used with foreach by reference' Any workaround? 回答1: Any workaround? Short answer: don't iterate-by-reference. This is an exception thrown by almost all of PHP's iterators (there are very few exceptions to this exception); it isn

初探PHP设计模式

一个人想着一个人 提交于 2019-12-04 04:46:12
设计模式不是一套具体的语言框架,是行之有效的编码规范,是 一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。合理使用设计模式将有助于初学者更加深入地理解面向对象思维。 一、三大基本模式 1、工厂模式   工厂模式分为简单工厂模式,工厂模式,抽象工厂模式,今天主要讲简单工厂模式。通过定义好的工厂方法或者类生成对象,而不是在代码中直接new 一个类来生成对象。使用工厂模式,可以避免当改变某个类的名字或者方法之后,在调用这个类的所有的代码中都修改它的名字或者参数。 声明一个初始类 class DataBase { public function __construct() { echo __CLASS__; } } 创建一个工厂类,写一个工厂方法 class Factory { static function creatDataBase() { $db = new DataBase(); return $db; } } 再在需要调用初始类的php代码处使用工厂类 spl_autoload_register('autoload1'); $db = SPL\Factory::creatDataBase(); function autoload1($class) { $dir = __DIR__;

Count elements for objects implementing ArrayAccess using count()?

不羁岁月 提交于 2019-12-04 03:24:08
问题 When a class implements the ArrayAccess interface, it becomes ready to function as an array, complete with OffsetGet, OffsetSet and so on. One thing I didn't see was an implementation for when we want to count() or sizeof() it, which, in my limited knowledge of PHP, amounts to the same. Is there anything like it already implemented in standard PHP? 回答1: The correct way would be to implement the Countable interface Example #1 Countable::count() example <?php class myCounter implements

Sorting files per directory using SPL's DirectoryTreeIterator

我们两清 提交于 2019-12-03 23:16:07
I found a couple of questions ( this one and this question ) related to the SPL iterators, but I'm not sure if they're helpful in my case, as I'm using a rather high level extension of the RecursiveIteratorIterator ; the DirectoryTreeIterator . Could somebody perhaps show me how to alter the DirectoryTreeIterator or how to sort the returned array per directory after it has been outputted by the iterator? A method of sorting the files correctly directly on the Apache server is also an option for me, if it's possible using .htaccess for example. This is the code of DirectoryTreeIterator from the

Does really SplFixedArray perform better than arrays?

大城市里の小女人 提交于 2019-12-03 12:13:13
问题 I'm testing the SplFixedArray building an array with the days of the week, and I get the following results: <?php $days = new SplFixedArray(7); $days[0] = "Monday"; $days[1] = "Tuesday"; $days[2] = "Wednesday"; $days[3] = "Thursday"; $days[4] = "Friday"; $days[5] = "Saturday"; $days[6] = "Sunday"; echo memory_get_peak_usage() . "\n"; //Returns 327688 echo memory_get_usage() . "\n"; //Returns 327140 echo memory_get_peak_usage(true) . "\n"; //Returns 524288 echo memory_get_usage(true) . "\n"; /

SPLFileInfo: get filename without extension

ⅰ亾dé卋堺 提交于 2019-12-03 11:36:00
I'm accessing a number of files in the SPLFileInfo object. I see a way to get the path, filename, and even extension of the file. Is there a way to get the filename without extension? Here's the code I've been working with but I'm hoping to get something more elegant. Is there an out of the box solution? $file = new SplFileInfo("path/to/file.txt.zip"); echo 'basename: '.$file->getBasename(); echo PHP_EOL; echo 'filename: '.$file->getFilename(); echo PHP_EOL; echo 'extension: '.$file->getExtension(); echo PHP_EOL; echo 'basename w/o extension: '.$file->getBasename('.'.$file->getExtension()); >

Spl, ArrayObject, ArrayObject::STD_PROP_LIST

六月ゝ 毕业季﹏ 提交于 2019-12-03 10:58:28
问题 I'm trying to understand STD_PROP_LIST constant in the documentation but so far i didn´t understand it, and didn´t found any explanation :( The documentation has the following example: $a = new ArrayObject(array(), ArrayObject::STD_PROP_LIST); $a['arr'] = 'array data'; $a->prop = 'prop data'; $b = new ArrayObject(); $b['arr'] = 'array data'; $b->prop = 'prop data'; // ArrayObject Object // ( // [prop] => prop data // ) print_r($a); // ArrayObject Object // ( // [arr] => array data // ) print

Why am I getting Fatal error when calling a parent's constructor?

半腔热情 提交于 2019-12-03 09:29:10
I am extending one of the SPL (Standard PHP Library) classes and I am unable to call the parent's constructor. Here is the error I am getting: Fatal error: Cannot call constructor Here is a link to the SplQueue 's documentation: http://www.php.net/manual/en/class.splqueue.php Here is my code: $queue = new Queue(); class Queue extends SplQueue { public function __construct() { echo 'before'; parent::__construct(); echo 'I have made it after the parent constructor call'; } } exit; What could prevent me from calling the parent's constructor? SplQueue inherits from SplDoublyLinkedList . Neither of

InvalidArgumentException vs UnexpectedValueException

僤鯓⒐⒋嵵緔 提交于 2019-12-03 07:25:45
问题 When should I use InvalidArgumentException and when UnexpectedValueException? They look the same to me. Note that one extends LogicException and the other one extends RuntimeException, so the difference shouldn't be so subtle IMO. 回答1: Looking closely at the descriptions on the manual pages: InvalidArgumentException Exception thrown if an argument is not of the expected type . (The description was Exception thrown if an argument does not match with the expected value. until mid-2014, but was

Extending ArrayObject in PHP properly?

喜你入骨 提交于 2019-12-03 07:23:17
Problem: I am trying to extend PHP's ArrayObject as shown below. Unfortunately I can't get it to work properly when setting multi-dimensional objects and instead an error thrown as I have the strict settings enabled in PHP. ( Error: Strict standards: Creating default object from empty value ) Question: How can I modify my class to automatically create non-existing levels for me? The code: $config = new Config; $config->lvl1_0 = true; // Works $config->lvl1_1->lvl2 = true; // Throws error as "lvl1" isn't set already class Config extends ArrayObject { function __construct() { parent::__construct