【问题标题】:Set Property in Static Class在静态类中设置属性
【发布时间】:2014-04-04 00:55:36
【问题描述】:

我有这个代码:

<?php

class test {
    public static function plus($input) {
        $conf = variable_get('config');
        $b = $conf['var'];
        return (int)$input + (int)$b;
    }

    public static function minus($input) {
        $conf = variable_get('config');
        $b = $conf['var'];
        return (int)$input - (int)$b;
    }
}

我想在属性中设置配置,而不是调用variable_get 在每个方法中加载配置,所以我可以在所有方法中调用它。 如何创建它?我尝试创建public function __construct() {} 并设置属性,但仍然无法在方法中调用它。

谢谢,

【问题讨论】:

  • 什么是variable_get?这是drupal吗?

标签: php static-classes


【解决方案1】:

试试这个

<?php
    function variable_get($p) {
    $arr = array('config' => array('var' => 4));
    return $arr[$p];
    }
    class test {
        public static $config_var = array();
        public static function plus($input) {
        $conf = self::$config_var;
        $b = $conf['var'];
        return (int)$input + (int)$b;
    }

    public static function minus($input) {
        $conf = self::$config_var;
        $b = $conf['var'];
        return (int)$input - (int)$b;
    }
}

test::$config_var = variable_get('config');
echo test::plus(12);
echo test::minus(12);


?>

【讨论】:

    【解决方案2】:

    对于加载和获取配置或设置文件,您可以使用 parse_ini_file :

    parse_ini_file — 解析配置文件

    示例#1 sample.ini 的内容

    ; This is a sample configuration file
    ; Comments start with ';', as in php.ini
    
    [first_section]
    one = 1
    five = 5
    animal = BIRD
    
    [second_section]
    path = "/usr/local/bin"
    URL = "http://www.example.com/~username"
    
    [third_section]
    phpversion[] = "5.0"
    phpversion[] = "5.1"
    phpversion[] = "5.2"
    phpversion[] = "5.3"
    

    index.php 的内容

    <?php
    
    define('BIRD', 'Dodo bird');
    
    // Parse without sections
    $ini_array = parse_ini_file("sample.ini");
    print_r($ini_array);
    
    // Parse with sections
    $ini_array = parse_ini_file("sample.ini", true);
    print_r($ini_array);
    
    ?>
    

    上面的例子会输出类似于:

    Array
    (
        [one] => 1
        [five] => 5
        [animal] => Dodo bird
        [path] => /usr/local/bin
        [URL] => http://www.example.com/~username
        [phpversion] => Array
            (
                [0] => 5.0
                [1] => 5.1
                [2] => 5.2
                [3] => 5.3
            )
    
    )
    Array
    (
        [first_section] => Array
            (
                [one] => 1
                [five] => 5
                [animal] => Dodo bird
            )
    
        [second_section] => Array
            (
                [path] => /usr/local/bin
                [URL] => http://www.example.com/~username
            )
    
        [third_section] => Array
            (
                [phpversion] => Array
                    (
                        [0] => 5.0
                        [1] => 5.1
                        [2] => 5.2
                        [3] => 5.3
                    )
    
            )
    
    )
    

    简单的类定义

    <?php
    class SimpleClass
    {
        // property declaration and access from all method
        public $var = 'a default value';
        public $ini_array = parse_ini_file("sample.ini");
    
    
        // method declaration
        public function displayVar() {
            echo $this->var;
            print_r($this->$ini_array);
        }
    }
    
    $Simpleclass = new SimpleClass();
    $Simpleclass->displayVar();
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多