Skip to content

Instantly share code, notes, and snippets.

@jun-ya
Created September 25, 2013 16:21
Show Gist options
  • Save jun-ya/6702139 to your computer and use it in GitHub Desktop.
Save jun-ya/6702139 to your computer and use it in GitHub Desktop.
FuelPHPでMongoDB使ってるときに調子付いてメソッドチェーンなんかつかってしまうと無駄に横長になってしまう(こんな書き方するなよって話だけど)。けど、php5.4から連想配列が ['foo'=>'bar'] でイケるようになったのでちょっとだけ短く表現できるよ。"array"、"("、")" をタイプするの面倒だもんね(笑)。
<?php
class Controller_Top extends Controller
{
/**
* 連想配列を array('foo' => 'bar') で表現したときのダメなメソッドチェーン例。
*/
public function action_mongotest()
{
$db = \Mongo_Db::instance();
$res = $db->where(array('foo' => 'bar', 'hoge' => 'huga'))->select(array('hoge_id', 'created_at'))->order_by(array('created_at' => 'asc'))->get('foo_bar');
// ...
}
/**
* php5.4からは array('foo' => 'bar') が ['foo' => 'bar'] と書けるようになったのでちょっとだけ短くなる。
*/
public function action_mongotestphp54()
{
$db = \Mongo_Db::instance();
$res = $db->where(['foo' => 'bar', 'hoge' => 'huga'])->select(['hoge_id', 'created_at'])->order_by(['created_at' => 'asc'])->get('foo_bar');
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment