Cでは(私がよく覚えていれば)それを行うことができます:
void foo()
{
static bool firstCall = true;
if(firstCall)
{
// stuff to do on first call
firstCall = false;
}
// more stuff
}
同じメソッドが複数回呼び出されたときにモデルがデータベースに複数回クエリを実行しないように、PHPでこれを実行したいと思います。
class User
{
public static function & getAll($reload = false)
{
static $result = null;
if($reload && null === $result)
{
// query the database and store the datas in $result
}
return $result;
}
}
許可されていますか?それは機能しますか?PHP <5.3と互換性がありますか?
はいの場合、私は別の質問があります:
すべてのモデルに共通するいくつかのメソッドがあるとしましょう。それらを抽象基本クラスにグループ化します。
abstract class AbstractModel
{
public static function & getAll($tableName, $reload = false)
{
static $result = array();
if($reload && !isset($result[$tableName]))
{
// query the database depending on $tableName,
// and store the datas in $result[$tableName]
}
return $result[$tableName];
}
}
class User extends AbstractModel
{
public static function & getAll($reload = false)
{
$result = parent::getAll('users', $reload);
return $result;
}
}
class Group extends AbstractModel
{
public static function & getAll($reload = false)
{
$result = parent::getAll('groups', $reload);
return $result;
}
}
これも機能しますか?改善できますか?
ご協力いただきありがとうございます :)