I recently started learning Ruby and was amazed at the sheer volume of incredibly efficient functionality that every single item had immediately available to it. Virtually anything that I wanted to do to a string, array, integer, etc was available without writing an extra function. Even something as simple as reversing the characters in a string went from this:
<?php
echo implode( array_reverse( str_split( $string ) ) );
// OR
echo strrev( $string );
?>
To this:
puts string.reverse
And this gave me an idea. What if I brought this level of convenience and functionality to PHP? And thus, RubyPHP was born.
The list of functionality is very, very long ( 66 functions and counting ), but it will be virtually constantly added to as time permits. Help from the community is also appreciated :-P
- PHP 5.3+
- Excitement
- Include the script
require_once('class.rubyphp.php');
- Remember to include your new PHP file in the
PierceMoore\RubyPHP
namespace. Simply includenamespace PierceMoore\RubyPHP;
at the top of your scripts and you will be in! - Whenever you create or generate data that you want to handle in cool ways, simply instantiate the object with the data inside it. You do NOT need to specify a data type. Just instantiate and the code works for you!
$foo = r("bar");
- Now that it's instantiated, you simply need to access some of the built-in properties that are created upon instantiation or, if you need something more thorough, run one of the awesome built-in functions.
echo $foo->length
orecho $foo->flip
. The possibilities are virtually endless.
#Samples - examples using data returned from a database
$data = $this->db->query('SELECT * FROM `users`'); // Returns an associative array of 10 items
$f = r($data);
echo $f->length; // 10
print_r( $f->flip ); // Shows the array reversed
print_r( $f->slashes("strip")); // Recursively iterates through the array and removes the slashes from every single item for display
print_r( $f->flatten ); // Flattens the array to one dimension and returns it
print_r( $f->first ); // Displays $data[0]
print_r( $f->last ); // Displays $data[9]
And many, many more!!
$foo = r( $data );
Data retrieved, let's remove the slashes we added earlier for security purposes. This function returns the $this
object if chaining is enabled (default: enabled) so you can just say:
print_r( $foo->slashes('strip')->val );
Let's loop through the entire array. Write your function here just as easily as you always would. The function you write will be applied to each and every member of the array.
NOTE: When you write your function, we have to work within PHP 5.3's limitations. If you are NOT running PHP 5.4, you are very limited with this function as you cannot access the $this
object within the anonymous function. PHP 5.3- should stick to the alternative syntax listed below.
$foo->each( function( $val ) {
$this->output .= "<tr><td>{$val['name']}</td><td>{$val['email']</td></tr>";
});
// Loop over, now we can access that data using $this->output;
echo "<table>{$this->output}</table>";
Alternative syntax, if you want to keep it closer to what you're used to OR if you are using a PHP version of less than 5.4:
foreach( $foo->val as $k => $v ) {
$output .= "<tr><td>{$v['name']}</td><td>{$v['email']</td></tr>";
}
echo "<table>$output</table>";
Perhaps you wanted to do cool things to each individual item during that loop. Let's try it.
foreach( $foo->val as $k => $v ) {
$name = r($v['name'])->cap("first")->val;
$pass = r($v['password'])->md5()->val; // OR $p = $pass->md5; OR $p = $pass->secure('sha512') // NOTE: In the secure() function, you can supply whatever hashing algorithm you want. tiger160,3 , crc32, whatver you want.
$email = r($v['email'])->tr()->val;
print "Name: $n, Pass: $p, Email: $e.";
}
There are lots of cool functions in here just for numbers. A few of my favorites are:
Integers
$foo = r(12345);
print $foo->money; // $12,345.00
print $foo->even; // false
print $foo->odd; // true
print $foo->mult(2); // 24690
print $foo->NaN; // false
Double / Float
$foo = r(12345.66);
print $foo->money; // $12,345.67
print $foo->even; // true
print $foo->odd; // false
print $foo->mult(2); // 24691.32
print $foo->NaN; // false