Question

I have this code:

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

instead of call variable_get to load configuration in each method, i want to set configuration in property, so I can call it inside all methods. how to create it? I tried to create public function __construct() {} and set the property, but still can not call it inside methods.

thanks,

Was it helpful?

Solution

Try this

<?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);


?>

OTHER TIPS

for load and get config or setting file you can using parse_ini_file :

parse_ini_file — Parse a configuration file

Example #1 Contents of 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"

Contents of 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);

?>

The above example will output something similar to:

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
                )

        )

)

Simple Class definition

<?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();
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top