Ruby / JQuery Abstraction layer for PHP
Posted: Thu Nov 23, 2006 3:14 pm
Hey guys. Let me know what you think of this concept project that i've been working on for the past week.
It is a framework to make PHP more OO in a dynamic way.
Here is what the framework can do
The reason i came up with this concept was because I was sick of checking the PHP website to see if it's
strstr($email, '@') or strstr('@', $email) and str_replace() or strreplace() etc. You get the idea. The inconsistency.
So now, with this lib, i can do this instead
How this works is that in my lib, there are a lot of modules. Each module has an accept() function to test if the variable passed to the p() or pp() functions can be used with that module. Its like a bastardized version of the Visitor pattern.
Other potential uses
So what do you think? It's more towards JQuery style than Ruby. But I name it Pruby nevertheless becos it's cooler 
To see all the methods available for a variable, you can do this
p($str)->codegen_registered_methods(); // Output is var_export of the methods available
or
p($str)->puts_registered_methods() or p($str)->dump_registered_methods(); // echo and var_dump respectively
Diff type of vars will have diff methods available.
P($str) will have access to string functions like trim(), capitalize() but not get_class()
p($obj) will have access to object functions like get_class() but not trim()
Here is the lib.
http://karl.stephen.googlepages.com/prubyism.zip
You need to:
set_include_path('path/to/pruby');
function __autoload($class)
{
if (substr($class, 0, 5) == 'Pruby') Pruby::load_class($class);
}
Then,
require('Pruby.php');
I haven't actually created many modules yet. Just the string, array and object module.
Those are core modules. For optional addon modules, i created a demo Zend module.
For those who have the Zend framework, you can do this.
echo p('<p>hello</p>')->no_tags(); // Output: 'hello'
Because it is an addon. It is not loaded by default. You need to add this line after you include the Pruby.php file
Pruby::add_mods(array('Addon_Zend'));
So let me know your thoughts about it yeah?
It is a framework to make PHP more OO in a dynamic way.
Here is what the framework can do
Code: Select all
while (p(10)->times()) { echo 'Echo me 10 times'; }
p() is like the $() in JQuery.
$str = ' he MAN leet ';
pp($str)->trim()->capitalize_words();
echo $str; // output: 'He Man Leet'
The difference b/w p and pp is that pp is passed by reference whereas p is pass by value.
So. the above is the same as: $str = p($str)->trim()->capitalize_words()->spit();
Spit() will return the variable after processing it.strstr($email, '@') or strstr('@', $email) and str_replace() or strreplace() etc. You get the idea. The inconsistency.
So now, with this lib, i can do this instead
Code: Select all
p($email)->has('@') and pp($str)->replace('okok');
That's for strings. For objects, you can do this.
p($obj)->get_class();
p($obj)->get_interfaces();
For arrays
pp($arr)->pop()->shift()->append('hello')->prepend('world);
And for shortcuts,
instead of
if (!isset($arr['abc']))
{
$arr['abc'] = 'some value';
}
you can do this one-liner: pp($arr)->set_default('abc', 'some value');Other potential uses
Code: Select all
echo pp($str)->trim()->capitalize_words()->to_leet(); // output: 'H3 Man L33t'
echo p('goog')->get_stock_quote(); // Fetch stock quote from Yahoo
echo p('hello')->to_french(); // output: 'bonjour'
echo pp($email)->js_encode(); // Output: Javascript encoded emailTo see all the methods available for a variable, you can do this
p($str)->codegen_registered_methods(); // Output is var_export of the methods available
or
p($str)->puts_registered_methods() or p($str)->dump_registered_methods(); // echo and var_dump respectively
Diff type of vars will have diff methods available.
P($str) will have access to string functions like trim(), capitalize() but not get_class()
p($obj) will have access to object functions like get_class() but not trim()
Here is the lib.
http://karl.stephen.googlepages.com/prubyism.zip
You need to:
set_include_path('path/to/pruby');
function __autoload($class)
{
if (substr($class, 0, 5) == 'Pruby') Pruby::load_class($class);
}
Then,
require('Pruby.php');
I haven't actually created many modules yet. Just the string, array and object module.
Those are core modules. For optional addon modules, i created a demo Zend module.
For those who have the Zend framework, you can do this.
echo p('<p>hello</p>')->no_tags(); // Output: 'hello'
Because it is an addon. It is not loaded by default. You need to add this line after you include the Pruby.php file
Pruby::add_mods(array('Addon_Zend'));
So let me know your thoughts about it yeah?