spl

Sleep from within an Informix SPL procedure

What's the best way to do the semantic equivalent of the traditional sleep() system call from within an Informix SPL routine? In other words, simply "pause" for N seconds (or milliseconds or whatever, but seconds are fine). I'm looking for a solution that does not involve linking some new (perhaps written by me) C code or other library...

PHP's SPL: Do its interfaces involving arrays cover all array properties?

Would it be possible to write a class that is virtually indistinguishable from an actual PHP array by implementing all the necessary SPL interfaces? Are they missing anything that would be critical? I'd like to build a more advanced Array object, but I want to make sure I wouldn't break an existing app that uses arrays everywhere if I s...

Remove repetitive, hard coded loops and conditions in PHP

I saw this question asked about C# I would like an answer for PHP. I have some old code that has 4 pages of foreach loops and conditions which just makes it hard to read and follow. How would I make this more OO? I was thinking of using SPL Functions but don't fully understand whats involved yet. ...

PHP objects as faux-arrays

I have an object that implements ArrayAccess, Iterator and Countable. That produces a nigh-perfect array masking. I can access it with offsets ($object[foo]), I can throw it into a foreach-loop, and many other things. But what I can't do is give it to the native array iterator functions (next(), reset(), current(), key()), even though I...

Iterating over a member array with IteratorAggregate

I have a class and I want to be able to iterate over a certain array member. I did a quick search and found IteratorAggregate: class foo implements IteratorAggregate { protected $_array = array('foo'=>'bar', 'baz'=>'quux'); public function getIterator() { return new ArrayIterator($this->_array); } } which work...

Which implementation of Iterator should I use in PHP, and why?

I'm trying to refactor a large, old project and one thing I've noticed is a range of different Iterator implementations: while($iterator->moveNext()) { $item = $iterator->current(); // do something with $item; } for($iterator = getIterator(), $iterator->HasNext()) { $item = $iterator->Next(); // do something with $it...

How do we get coders to look up existing functions before writing their own?

Why are so many people still writing crappy versions of things in standard libraries? Not to go after PHP developers, but guys go read the PHP SPL ...

PHP SPL to manipulate recursive menu

I have a table in a database which has category data: id title parent 1 category 1 0 2 category 2 2 3 category 3 3 4 category 4 0 Eeach parent may have the parent row id. For example, category 3 is a child of category 2, which is child of category 1. category 1 category 2 category 3 category 4 ...

how to find what languages are loaded into EnterpriseDB?

How can I find what languages have been loaded into EnterpriseDB(PL/pgsql, SPL, Java)? EnterpriseDB is built on top of PostgreSQL if anyone knows of a way to find the loaded languages on PostgreSQL. It should work the same. ...

How do I alter array keys and values while using a RecursiveArrayIterator?

I suspect I'm doing something stupid here, but I'm confused by what seems like a simple problem with SPL: How do I modified the contents of an array (the values in this example), using a RecursiveArrayIterator / RecursiveIteratorIterator? Using the follow test code, I can alter the value within the loop using getInnerIterator() and off...

__autoload mix up?

I have a server with many customers on, when I develop I include my init.php in which I have an __autoloader() function that includes the file with dir_name(__FILE__)."/classes/".$className for instance. But yesterday I saw that the server could not find the specific class, I restartat apache and then it worked again. Every customer ha...

How does the PHP IteratorIterator class work?

Try as I might I cannot get my head around what the IteratorIterator class actually does. I understand that classes can implement Traversable so the engine knows it can loop using foreach and I realise that IteratorIterator is supposed to convert anything that is Traversable into an Iterator but I cannot for the life of me understand how...

Sorting files with DirectoryIterator

Hi, I'm making a directory listing PHP5 script for lighttpd. In a given directory, I'd like to be able to list direct sub-directories and files (with informations). After a quick search, DirectoryIterator seems to be my friend: foreach (new DirectoryIterator('.') as $file) { echo $file->getFilename() . '<br />'; } but I'd like t...

iterator_to_array

DatePeriod is a PHP class for handling recurring dates. It has a very limited number of methods. So when I want to do basic array functions with the recurring dates, I have to copy it to an array with iterator_to_array. Strangely, copying it seems to clobber it. Any ideas why? $p=new DatePeriod(date_create('2008-01-01'), ...

SplObjectStorage doesn't work with String, what to do?

Someone has suggested to e to use SplObjectStorage to keep track of a set of unique things. Great, except it doesn't work with strings. An error says " SplObjectStorage::attach() expects parameter 1 to be object, string given in fback.php on line 59" Any ideas? ...

PHP: SPLFileObject next() behavior

Hi there, In PHP SPLFileObject allows treating files as iterators. Yet there is a behavior that I don't understand. When you call next() on the object it increments the value of key() but does not advance the line in the file unless you call current() with each iteration. The SPL docs state that key() returns current line number. Code...

PHP array_key_exists() and SPL ArrayAccess interface: not compatible?

I wrote a simple collection class so that I can store my arrays in objects: class App_Collection implements ArrayAccess, IteratorAggregate, Countable { public $data = array(); public function count() { return count($this->data); } public function offsetExists($offset) { return (isset($this...

SPL Throwing Exceptions

Is there a way to throw exceptions from an SPL Autoloader in PHP in case it fails? It doesn't seem to work under PHP 5.2.11. class SPLAutoLoader{ public static function autoloadDomain($className) { if(file_exists('test/'.$className.'.class.php')){ require_once('test/'.$className.'.class.php'); return...

Make an object searchable with array_search in PHP

Hi, I would like to make a class in PHP such as it would be searchable with PHP native method array_search. Currently my class implements IteratorAggregate and Countable, which allows me to do foreach on it. There is a couple of other SPL interfaces (SeekableIterator, ArrayAccess, etc...) which seems to maybe fit, but I would like to k...

SPL Autoloading best practices

In my include_path on the server-side I have a reference to a pear directory, in '/usr/share/pear/'. Within my applications I include files from a common library, living in '/usr/share/pear/library/' with require_once 'library/file.php'. I've recently started using the spl autoloader, I noticed in the loader function you have to determ...