【问题标题】:Dynamically populating a static variable in PHP在 PHP 中动态填充静态变量
【发布时间】:2011-05-16 22:46:51
【问题描述】:

我有两个静态值:“type”和“typeID”。类型是人类可读且恒定的,需要根据类型的值从数据库中查找类型ID。我需要在第一次加载类定义时进行一次查找

为了说明,这里有一些代码不起作用,因为您不能在声明空间中调用函数。

MyClass extends BaseClass {
  protected static $type = "communities";
  protected static $typeID = MyClass::lookupTypeID(self::$type);
}

在加载类定义时是否有一个神奇的方法只调用一次?如果有明显的东西,我会错过它。

【问题讨论】:

  • @webbiedave - 这是关于初始化的,它恰好是相同的根本原因,但我认为问题是不同的。
  • 填充,初始化。番茄,番茄 :) 我认为它是一样的,但我严重怀疑它是否会在此时关闭。

标签: php object static


【解决方案1】:

无耻地从php手册的静态关键字cmets中拉出来:

Because php does not have a static constructor and you may want to initialize static class vars, there is one easy way, just call your own function directly after the class definition.

for example.

<?php
function Demonstration()
{
    return 'This is the result of demonstration()';
}

class MyStaticClass
{
    //public static $MyStaticVar = Demonstration(); //!!! FAILS: syntax error
    public static $MyStaticVar = null;

    public static function MyStaticInit()
    {
        //this is the static constructor
        //because in a function, everything is allowed, including initializing using other functions

        self::$MyStaticVar = Demonstration();
    }
} MyStaticClass::MyStaticInit(); //Call the static constructor

echo MyStaticClass::$MyStaticVar;
//This is the result of demonstration()
?>

【讨论】:

    【解决方案2】:

    简单且不需要魔法,不要忘记您始终可以将变量定义为 null 并测试它是否为 null(然后才进行 db 调用)。然后,如果您希望在构造或包含类时发生这种情况(include_once 等...),这只是一个问题

    MyClass extends BaseClass {
        protected static $type = "communities";
        protected static $typeID = null;
    
        public function __construct(){
            if(is_null(self::$typeID)){
                self::lookupTypeID(self::$type);
            }
        }
    
        public static lookupTypeID($type){
            self::$typeID = //result of database query
        }
    }
    

    MyClass::lookupTypeID(); //call static function when class file is included (global space)
    
    MyClass extends BaseClass {
        protected static $type = "communities";
        protected static $typeID = null;
    
        public function __construct(){
    
        }
    
        public static lookupTypeID($type=null){
            if(is_null($type)){
                $type = self::$type;
            }
            self::$typeID = //result of database query (SELECT somefield FROM sometable WHERE type=$type) etc..
        }
    }
    

    静态构造函数更像是工厂方法

    if(!function_exists(build_myclass)){
        function build_myclass(){
            return MyClass::build();
        }
    }
    
    MyClass extends BaseClass {
        protected static $type = "communities";
        protected static $typeID = null;
    
        public function __construct(){
    
        }
    
        public static function build(){
            return new self(); //goes to __construct();
        }
    
     }
    
    $class = new MyClass(); //or
    $class = MyClass::build(); //or
    $class = build_myclass();
    

    【讨论】:

      【解决方案3】:

      这样的东西通常被称为“静态构造函数”,但是 PHP 缺少这样的东西。您可能需要考虑 PHP 手册 cmets 中建议的一种解决方法,例如http://www.php.net/manual/en/language.oop5.static.php#95217

      【讨论】:

        猜你喜欢
        • 2014-12-07
        • 2018-07-04
        • 2018-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多