I’m not sure exactly what the point of this is, but I’ve basically reconstructed a String object in PHP.
It requires PHP5, as it uses Interfaces:
<?php
/*************************************
/* PRIMITIVE CLASSES
* -----------------
*
* Basically, this extends the default
* functionality of primitives and
* their related functions to be more
* OOP. I'm not sure exactly what use
* it has yet...
************************************/
class String implements Iterator {
/* Future Features:
* Ha! Caught up for now...
*/
public $string;
private $position;
public function __construct ($string) {
$this->string = (string) $string;
}
public function getStr () { return $this->string; }
public function __toString () { return $this->string; }
/** STRING FUNCTIONS **/
// The destructive ones
public function prepend ($str) { $this->string = $str.$this->string; }
public function append ($str) { $this->string .= $str; }
// The non-destructive ones
public function strmsk ($char_list) {
// Masks string to only contain letters in $char_list
$haystack = str_split($this->string);
$newstr = array(); // Array containing correct string
foreach($haystack as $letter) {
if(strpos($char_list,$letter)!==false) $newstr[] = $letter;
}
return implode('',$newstr);
}
// Iterator functions
public function rewind () { $this->position=0; }
public function current () { return $this->string{$this->position}; }
public function key () { return $this->position; }
public function next () { return $this->string{$this->position++}; }
// Post-transform to include all letters in loop
public function valid () { return ($this->position<strlen($this->string)); }
}
?>
Any suggestions for making this useful? Well, currently I find it really useful, as you can iterate through a string… I’ve always wanted this. But anything else?