Skip to content

shaunxcode/phutility

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

 ____     __  __   __  __   ______  ______   __     ______  ______  __    __ 
/\  _`\  /\ \/\ \ /\ \/\ \ /\__  _\/\__  _\ /\ \   /\__  _\/\__  _\/\ \  /\ \
\ \ \L\ \\ \ \_\ \\ \ \ \ \\/_/\ \/\/_/\ \/ \ \ \  \/_/\ \/\/_/\ \/\ `\`\\/'/
 \ \ ,__/ \ \  _  \\ \ \ \ \  \ \ \   \ \ \  \ \ \  __\ \ \   \ \ \ `\ `\ /' 
  \ \ \/   \ \ \ \ \\ \ \_\ \  \ \ \   \_\ \__\ \ \L\ \\_\ \__ \ \ \  `\ \ \ 
   \ \_\    \ \_\ \_\\ \_____\  \ \_\  /\_____\\ \____//\_____\ \ \_\   \ \_\
    \/_/     \/_/\/_/ \/_____/   \/_/  \/_____/ \/___/ \/_____/  \/_/    \/_/

Phutility is a collection of tools to make life in php more livable. Primarily
it is meant to scratch the itch of a beaver who want's to play guitar but is 
stuck trying to play keyboard whilst pretending to be a duck 9-5. 
http://30.media.tumblr.com/tumblr_l3wek2i9FK1qzev6go1_400.jpg

Invokable is a utility for creating simple callbacks when dealing w/ objects.

$collection->map(function($i) use($var1, $var2) { 
    return $i->someMethod()->someOther($var1)->someOther($var2);
});

Becomes:

use Phutility\Invokable as I;

$collection->map(I::someMethod()->someOther($var1)->someOther($var2));

As you may have inferred Invokable is a class which builds up a stack using __call and __get
and then further, implements the __invoke method so it may be used in any context
which an anonymous function would other wise. 

Further:

$x = I::callMethod(30)->secondMethod($arg2);

$x($y);
$x($z);

Which is equivalent to:

$y->someMethod(30)->secondMethod($arg2);
$z->someMethod(30)->secondMethod($arg2);

Appos is useful for when you want to do things that would previously require
passing around associative arrays i.e.

$f = function($options) { 
	echo $options['name'] . " is " . $options['age'];
};

$f(array('name' => 'peter', 'age' => 50));

Can now be: 
$f = function($options) { 
	$options = Appos::create($options);
	echo $options['name'] . " is " . $optjons['age'];
};

$f('name', 'peter', 'age', 50);

Or even better you can use a higher order function like options or rest: 

use Phutility\Func as F;

Func gives us even more sugar:
$f = F::options(function($options) {
....
});

Rest args are fun too:
$f = F::rest(function($a, $b, $rest) {
	echo "a is $a with b of $b and " . json_encode($rest);
});

$f($obj1, $obj2, 
	'sortBy', 'col1', 
	'sortDir', 'desc');
	
Keyword args are super cool:

$keywordFunc = F::keyword(function($name, $age) {

});

$keywordFunc(
	age, 34,
	name, 'peter jensen');