简体   繁体   English

PHP静态类返回引用

[英]PHP static class return reference

I have class with a static method. 我有一个静态方法的类。 The static method returns a private static stdClass object. static方法返回私有静态stdClass对象。

myclass::get() // returns stdClass object
myclass::get()->name // name is hardcoded into the class

How would I change name's value like: 我如何更改名称的值,如:

myclass::get()->name = 'bob';

and have it set? 并设置好吗?

I tried returning the object like: 我尝试返回对象,如:

return &self::$static_object;

But that throws syntax errors. 但这会引发语法错误。

What can i do? 我能做什么?

EDIT posted code for clarification EDIT发布了代码以便澄清

final class config {

    private static $configs = array();

    public static function get($config_name) {

        if (isset($configs[$config_name])) {

            return self::$configs[$config_name];
        }

        $file = __get_file_exists(M_CONFIGS . $config_name, 'conf.');

        if ($file) {

            $config = self::__scope_include($file);

            if (!is_array($config) && !$config instanceof stdClass) {
                /*
                 * 
                 * 
                 * FIX
                 * 
                 * 
                 * 
                 */
                die('ERROR config.php');
            }

            return self::$configs[$config_name] = self::__to_object($config);
        }
    }

    private static function __scope_include($file) {

        return include $file;
    }

    private static function __to_object($config) {

        $config = (object) $config;

        foreach ($config as &$value) {

            if (is_array($value)) {

                $value = self::__to_object($value);
            }
        }

        return $config;
    }
}

echo config::get('people')->name; //dave
config::get('people')->name = 'bob';
echo config::get('people')->name; // should be bob, is dave

Returning by reference in the get() method should do the trick: get()方法中通过引用返回应该做的诀窍:

public static function &get() {
    return self::$static_object;
}

But, I think you should revisit your design, as this kind of coding is highly frowned upon and will cause maintenance and testability headaches down the road. 但是,我认为您应该重新审视您的设计,因为这种编码非常不受欢迎,并且会导致维护和可测试性问题。

What you are doing and the answer from drrcknlsn break Encapsulation . 你正在做什么, drrcknlsn的答案打破了封装 That is bad. 那很不好。

The correct way to do this is to create a setter method. 正确的方法是创建一个setter方法。

public static function set($key, $value) {
  // set $config property...
}

You missed self in if (isset($configs[$config_name])) { . 你错过了self if (isset($configs[$config_name])) { It should be 它应该是

if (isset(self::$configs[$config_name])) {
  return self::$configs[$config_name];
}

Otherwise each time you call config::get('people') , you will be reading your config file which most likely returns an array and convert it to an object before returning it. 否则,每次调用config::get('people') ,您将读取您的配置文件,该文件最有可能返回一个数组并在返回之前将其转换为对象。 Any changes you make to the object in self::$configs[$config_name] are overwritten by the newly created object. 您在self::$configs[$config_name]中对对象所做的任何更改都会被新创建的对象覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM