Saturday, 15 September 2012

php - Use Fluent Interface with less Code -



php - Use Fluent Interface with less Code -

how can cut down these 2 lines

$foo = new bar(); $baz = $foo->methodone('param')->methodtwo('param');

to

$baz = bar::methodone('param')->methodtwo('param');

i´ve seen specially in laravel , it´s nice readable code. stuck getting work custom helper-classes. feels mixing static + nonstatic functions confusing ...

laravel next way

in: vendor/laravel/framework/src/illuminate/database/capsule

/** * dynamically pass methods default connection. * * @param string $method * @param array $parameters * @return mixed */ public static function __callstatic($method, $parameters) { homecoming call_user_func_array(array(static::connection(), $method), $parameters); } /** * connection instance global manager. * * @param string $connection * @return \illuminate\database\connection */ public static function connection($connection = null) { homecoming static::$instance->getconnection($connection); }

from phpdoc:

__callstatic() triggered when invoking inaccessible methods in static context.

i think can simplify class:

class bar{ public static function __callstatic($method, $parameters) { homecoming call_user_func_array(array(new bar(), $method), $parameters); } public function hello(){ echo "hello"; } } bar::hello();

php oop laravel fluent-interface

No comments:

Post a Comment