I seen some OOP of this, but one is to CPU intensive, and the other one... not that flexible.
Here is a way you can loop whatever you want, in whatever way you want.
The code could be re-factored a bit, but you should get the idea.
If OOP is the way to go, I find that you get more flexibility this way:
PHP Code:
/**
* Interface class for everything that needs to do stuff
*/
interface iStuff {
/**
* This function must do the magic, and return whatever needs to be returned.
*/
function execute();
}
PHP Code:
/**
* Class to do stuff to strings
*/
class cStringStuff implements iStuff {
/**
* My string to do stuff to
*
* @var String
*/
private $_string;
/**
* Counter to see how many time we executed this function.
*
* @var Integer
*/
private $_counter = 0;
/**
* Constructor: Takes in the string to do stuff to.
*
* @param String $string
*/
public function __construct($string) {
$this->_string = $string;
}
/**
* Function will do stuff to our string, and then return the result.
*
* @return String
*/
public function execute() {
$this->_counter += 1;
return join ( '', array (
"<br />\n",
$this->_counter,
": ",
join ( '*', preg_split ( "/\\b/", $this->_string ) )
) );
}
}
PHP Code:
/**
* Class will hold the for loop count
*/
class cLoop implements iStuff {
/**
* The number of times to execute this
*
* @var Integer
*/
private $_counts;
/**
* The object we want to loop
*
* @var iStuff
*/
private $_object;
/**
* Constructor: Takes in the count to loop.
*
* @param unknown_type $object The object we must loop.
* @param Integer $count How many times to loop this object.
*/
public function __construct(iStuff $object, $count = 10) {
$this->_counts = $count;
$this->_object = $object;
}
/**
* Decreases the count by one, and runs the object we must run.
*
* @return unknown Whatever the passed in object returns.
*/
public function execute() {
$this->_counts -= 1;
return $this->_object->execute ();
}
/**
* Return the current count.
*
* @return Integer
*/
public function getCount() {
return $this->_counts;
}
}
PHP Code:
/**
* Class will do something special on every loop count.
*/
class cSpecialLoop extends cLoop implements iStuff {
/**
* Decreases the count by one, and runs the object we must run.
*
* @return unknown Whatever the passed in object returns.
*/
public function execute() {
echo "<br />\nMy special loop ran!";
return parent::execute ();
}
}
PHP Code:
/**
* Controller class to loop all the loops
*
*/
class cFor implements iStuff {
/**
* The object we want to loop
*
* @var cLoop
*/
private $_object;
/**
* Holds our output buffer
*
* @var String
*/
private $_outputBuffer = '';
/**
* Constructor: Takes in the Looper object to use, if it should automatically
* execute it, and if it should automatically print out the result.
*
* @param cLoop $object Loop object to use.
* @param Boolean $autorun If we should execute automatically.
* @param Boolean $autoPrint If we should print out the result automatically.
*/
public function __construct(cLoop $object, $autorun = false, $autoPrint = false) {
$this->_object = $object;
if ($autorun) {
$this->execute ( $autoPrint );
}
}
/**
* Executes the loop code.
*
* @param Boolean $autoPrint If we should automatically print out the buffer.
*/
public function execute($autoPrint = false) {
if ($this->_object->getCount () > 0) {
$this->_outputBuffer .= $this->_object->execute ();
$this->execute ( $autoPrint );
} else if ($autoPrint) {
$this->printBuffer ();
}
}
/**
* Prints out the output buffer.
*/
public function printBuffer() {
echo $this->_outputBuffer;
}
}
And you can simply use it like this:
PHP Code:
# Create a new for loop, run it and print it, of 5 loops executing the cStringStuff object.
$myLooper = new cFor ( new cSpecialLoop ( new cStringStuff ( "Do something with this string" ), 5 ), true, true );
Which will output:
Code:
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
1: *Do* *something* *with* *this* *string*
2: *Do* *something* *with* *this* *string*
3: *Do* *something* *with* *this* *string*
4: *Do* *something* *with* *this* *string*
5: *Do* *something* *with* *this* *string*
Or of course like:
PHP Code:
$string = new cStringStuff ( "Do something with this other string" );
$loop = new cSpecialLoop($string, 10);
$for = new cFor($loop);
$for->printBuffer();
Which will output:
Code:
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
My special loop ran!
1: *Do* *something* *with* *this* *other* *string*
2: *Do* *something* *with* *this* *other* *string*
3: *Do* *something* *with* *this* *other* *string*
4: *Do* *something* *with* *this* *other* *string*
5: *Do* *something* *with* *this* *other* *string*
6: *Do* *something* *with* *this* *other* *string*
7: *Do* *something* *with* *this* *other* *string*
8: *Do* *something* *with* *this* *other* *string*
9: *Do* *something* *with* *this* *other* *string*
10: *Do* *something* *with* *this* *other* *string*
Remember kids, OOP is good for you, sometimes...
Bookmarks