spl

PHP - Initialize object members with array parameter

只愿长相守 提交于 2019-12-02 08:13:30
Is it possible to initialize an objects private or protected members in php with an associative array. for example: class TestClass { public $_name; public $_age; public function __construct(array $params) { ?????? } } $testClass = new TestClass( array( 'name' => 'Bob', 'age' => '29', ) ); i was wondering whether there is an elegant solution - perhaps by implementing one the spl interfaces or otherwise? You mentioned SPL. But without knowing the exact requirements for the purpose of your object, the below is about the only information I can give... You could have your object extend the SPL

PHP array_walk_recursive() for SimpleXML objects?

混江龙づ霸主 提交于 2019-12-01 22:00:55
问题 I would like to apply a function to every node in a SimpleXML object. <api> <stuff>ABC</stuff> <things> <thing>DEF</thing> <thing>GHI</thing> <thing>JKL</thing> </things> </api> //function reverseText($str){}; <api> <stuff>CBA</stuff> <things> <thing>FED</thing> <thing>IHG</thing> <thing>LKJ</thing> </things> </api> How would I apply reverseText() to every node to get the second XML snippet? 回答1: Here the Standard PHP Library can come to the rescue. One option is to use the (little known)

PHP array_walk_recursive() for SimpleXML objects?

雨燕双飞 提交于 2019-12-01 18:39:21
I would like to apply a function to every node in a SimpleXML object. <api> <stuff>ABC</stuff> <things> <thing>DEF</thing> <thing>GHI</thing> <thing>JKL</thing> </things> </api> //function reverseText($str){}; <api> <stuff>CBA</stuff> <things> <thing>FED</thing> <thing>IHG</thing> <thing>LKJ</thing> </things> </api> How would I apply reverseText() to every node to get the second XML snippet? Here the Standard PHP Library can come to the rescue. One option is to use the (little known) SimpleXMLIterator . It is one of several RecursiveIterator s available in PHP and a RecursiveIteratorIterator

Count elements for objects implementing ArrayAccess using count()?

与世无争的帅哥 提交于 2019-12-01 17:36:11
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? Gordon The correct way would be to implement the Countable interface Example #1 Countable::count() example <?php class myCounter implements Countable { public function count() { static $count = 0; return ++$count; } } $counter = new myCounter; for(

四则运算结对编程(JAVA)

早过忘川 提交于 2019-12-01 14:13:03
一、Github项目地址 https://github.com/lyjkekeke/Arithmetic 项目成员:刘昱君,潘蓓文 二、PSP表格 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 30 · Estimate · 估计这个任务需要多少时间 30 30 Development 开发 2340 2565 · Analysis · 需求分析 (包括学习新技术) 210 180 · Design Spec · 生成设计文档 90 120 · Design Review · 设计复审 (和同事审核设计文档) 60 30 · Coding Standard · 代码规范 (为目前的开发制定合适的规范) 60 45 · Design · 具体设计 500 400 · Coding · 具体编码 1000 1200 · Code Review · 代码复审 120 90 · Test · 测试(自我测试,修改代码,提交修改) 300 500 Reporting 报告 275 240 · Test Report · 测试报告 240 200 · Size Measurement · 计算工作量 15 15 · Postmortem & Process Improvement Plan ·

Is it possible to use SplEnum in php 5.2.6?

谁说胖子不能爱 提交于 2019-12-01 03:52:13
I tried to use class : abstract class my_abstractEnum extends SplEnum { ... } and class my_categoryEnum extends my_abstractEnum { ... } and I have : Fatal error: Class 'SplEnum' not found I work on PHP 5.2.6. SplEnum is for php > 5.3 ? I don't see so in the documentation ... SplTypes is an experimental PECL Extension . You have to install it with pecl install SPL_Types from the command line. There is no DLL for windows, so you are limited to Linux (or have to build your own). An alternative in userland can be found in http://www.whitewashing.de/2009/08/31/enums-in-php.html 来源: https:/

Is there any native PHP function which throws an built-in Exception?

假装没事ソ 提交于 2019-11-30 19:07:47
While answering PHP rename() doesn't throws exception on error I was wondering if there are any native PHP functions which throw a built-in Exception , beside the SPL stuff? Not really. If you read the note on that page you linked: Note: Internal PHP functions mainly use Error reporting , only modern Object oriented extensions use exceptions . However, errors can be simply translated to exceptions with ErrorException . PDO can be configured to throw exceptions PHP 5 has an exception model similar to that of other programming languages. ErrorException 来源: https://stackoverflow.com/questions

What are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays?

半腔热情 提交于 2019-11-30 12:14:56
I've started to learn PHP SPL from ArrayIterators and I'd like to know what are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays? a) I've heard that loops using SPL iterators will reduce memory usage (but why?). I don't really know to believe this or no because I don't understand how can it reduce memory usage. b) Talking about RecursiveArrayIterator we can say that sometimes it could save some lines of code (we're using one foreach construction instead of 2+ (depends on array dimension)). Probably, my questions could seem to be very easy

Why must I rewind IteratorIterator

岁酱吖の 提交于 2019-11-30 09:37:33
问题 $arrayIter = new ArrayIterator( array(1, 2) ); $iterIter = new IteratorIterator($arrayIter); var_dump($iterIter->valid()); //false var_dump($arrayIter->valid()); //true If I first call $iterIter->rewind() , then $iterIter->valid() is true. I'm curious why it requires that rewind() be called. I imagine there's good reason for it, but I would have expected it to simply start iteration at whatever state it's inner iterator is in, and leave it as an option to rewind before beginning iteration.

spl_autoload_register[$this, autoload]

只愿长相守 提交于 2019-11-30 05:57:11
我的疑问: spl_autoload_register([$this, 'autoload']) 平时见的都是$this-> ,写成这样[$this, autoload]该怎么理解 可以理解成spl_autoload_register([$this->autoload) 吗 [$this, autoload] 这个数组 是__aotuload这个魔术方法的变体写法吗 感谢学习群的 Find老师 详细的解答 示例 class test{   public static function loadprint($class){     $file = $class . '.class.php';     if(is_file($file)){       require_once($file);     }   } } spl_autoload_register( array('test', 'loadprint') ); //另一种写法:spl_autoload_register( "test::loadprint"); $obj = new PRINTIT(); $obj->doprint(); 来源: https://www.cnblogs.com/rjbc/p/11565648.html