【问题标题】:PHP - Variable static to a functionPHP - 函数的变量静态
【发布时间】:2012-11-23 22:52:44
【问题描述】:

在 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

如果是,那么我还有一个问题:

假设我们有几种所有模型共有的方法,我会将它们分组到一个抽象基类中:

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;
    }
}

这也行吗?可以改进吗?

感谢您的帮助:)

【问题讨论】:

  • 无关:为什么要返回参考文献?你应该让PHP优化代码,你通常不需要这样做。
  • 我通常返回数组,如果我理解得很好,它们被认为是原始类型,并被复制。我错了吗?
  • 只要你不修改它,编译器就会优化它而不复制数组。
  • @JuanMendes 这是一个新功能吗?我也遇到了数组被复制的麻烦。您可以通过引用传递它们或将它们包装在一个类中来解决它。
  • php.net/manual/en/language.references.return.phpDo not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.

标签: php function methods static


【解决方案1】:

是的,你可以这样做。自 PHP 5.0.0 起受支持。

为了澄清,我想区分 PHP 中两个非常不同的东西,它们都使用 static 关键字。首先是class static scope,它专门属于整个类。二是variable static scope,具体属于函数的局部作用域。

这是类静态作用域(仅在 PHP >= 5.3.0 中可用):

class Foo {
    public static $var;

    public static function GetVar() {
        return ++static::$var;
    }
}

var_dump(Foo::GetVar(),Foo::GetVar());

上面的代码会给你int(1) int(2),这是你所期望的。

这是变量静态作用域(在 PHP >= 5.0.0 中可用):

class Foo {
    public static function GetVar() {
        static $var = 0;
        return ++$var;
    }
}

var_dump(Foo::GetVar(),Foo::GetVar());

上面的代码也会给你int(1) int(2),这也是你所期望的。

请注意,一个专门属于函数的本地范围(即使该函数是类成员),另一个专门属于该类。

另外请注意,我在第一个示例中使用了 static 关键字,而不是 self 关键字,因为 self 不允许您执行 LSB (Late Static Binding)。这可能是您在继承然后调用父类时需要考虑的事情,特别是如果您要在函数的局部范围内使用类静态变量而不是静态变量。

【讨论】:

  • 感谢您的回答。这个似乎更准确地说明了我想知道的内容,因此我将更改已接受的答案:p 另外,您是否建议我使用类或变量静态范围来做到这一点?
  • 这取决于你的意图。如果您想要将该静态变量存储在函数的本地范围中,则使用变量静态范围。这样,您只能从该函数内部获取变量。如果您想与类中的其他函数共享该变量,请使用类静态范围,因为这样您就不必调用该函数来获取变量。
  • 我的空间用完了,所以我不得不添加这部分“您还可以使用 public、private 或 protected 关键字指定类变量的可见性。这使得该变量对所有人都可用( public),仅对类及其子类(受保护),或仅对该类(私有)。”
  • 我知道谢谢^^ 我已经使用PHP 一年了,在此之前从未遇到过涉及变量静态范围的问题:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
相关资源
最近更新 更多