PHP __get 重载返回数组而不是对象

发布于 2024-12-14 06:55:52 字数 2301 浏览 0 评论 0原文

为什么它返回一个数组而不是一个对象以及如何返回一个对象?

Class MyClass {
    private $filename = '...';
    private $_ini_array;

    public function __construct() {
        // Get the config file data
        $ini_array = parse_ini_file( $filename, true );
        $this->_ini_array = $ini_array;
    }

    public function __get( $param ) {
        return $this->_ini_array[ $param ];
    }
}

调用...

$c = new MyClass();
var_dump( $c->db_pgsql );

返回...

array(6) {
  ["data"]=>
  string(4) "test"
  ...

并铸造...

return (object) $this->_ini_array;

返回...

object(stdClass)#2 (6) {
  ["data"]=>
  string(4) "test"
  ...

而我希望返回...

object(MyClass)#2 (6) {
  ["data"]=>
  string(4) "test"
  ...

非常感谢!

更新。解决了。​​

我最终编写了以下课程,它几乎实现了我的目标。如果您看到任何坏习惯、草率的代码等,请发表评论。

class Config {
    private $config_filename = '../include/config.ini';


    public function __construct( array $array=null ){
        if ( $array ) {
            foreach ( $array as $key => $val ) {
                $this->$key = $val;
            }
        } else {
            $ini_array = parse_ini_file( $this->config_filename, true );

            foreach( $ini_array as $key => $val ){
                $this->$key = new self( $val );
            }
        }
    }


    public function __get( $param ) {
        return $this->$param;
    }
}

其中,使用我特定的测试配置文件,会生成一个看起来像......的对象...

VarDump: object(Config)#1 (3) {
    ["config_filename:private"]=>
    string(21) "../include/config.ini"
    ["heading1"]=>
    object(Config)#2 (3) {
        ["config_filename:private"]=>
        string(21) "../include/config.ini"
        ["str1"]=>
        string(4) "test"
        ["str2"]=>
        string(5) "test2"
    }
    ["heading2"]=>
    object(Config)#3 (2) {
        ["config_filename:private"]=>
        string(21) "../include/config.ini"
        ["str1"]=>
        string(9) "testagain"
    }
}

我宁愿不要递归地重复 ["config_filename:private"] 属性就像它所做的那样。但我想不出解决办法。因此,如果您知道解决方法,那么我将不胜感激。

感谢所有帮助我走向正确方向的帮助。

Why does this return an array instead of an object and how can I return an object?

Class MyClass {
    private $filename = '...';
    private $_ini_array;

    public function __construct() {
        // Get the config file data
        $ini_array = parse_ini_file( $filename, true );
        $this->_ini_array = $ini_array;
    }

    public function __get( $param ) {
        return $this->_ini_array[ $param ];
    }
}

called by...

$c = new MyClass();
var_dump( $c->db_pgsql );

returns...

array(6) {
  ["data"]=>
  string(4) "test"
  ...

and casting by...

return (object) $this->_ini_array;

returns...

object(stdClass)#2 (6) {
  ["data"]=>
  string(4) "test"
  ...

while I wish to return...

object(MyClass)#2 (6) {
  ["data"]=>
  string(4) "test"
  ...

Many Thanks!

Update. Solved.

I ended up writing the following class that pretty much accomplishes my goals. Please comment if you see any bad habits, sloppy code, etc.

class Config {
    private $config_filename = '../include/config.ini';


    public function __construct( array $array=null ){
        if ( $array ) {
            foreach ( $array as $key => $val ) {
                $this->$key = $val;
            }
        } else {
            $ini_array = parse_ini_file( $this->config_filename, true );

            foreach( $ini_array as $key => $val ){
                $this->$key = new self( $val );
            }
        }
    }


    public function __get( $param ) {
        return $this->$param;
    }
}

Which, with my particular test config file, produces an object that looks like...

VarDump: object(Config)#1 (3) {
    ["config_filename:private"]=>
    string(21) "../include/config.ini"
    ["heading1"]=>
    object(Config)#2 (3) {
        ["config_filename:private"]=>
        string(21) "../include/config.ini"
        ["str1"]=>
        string(4) "test"
        ["str2"]=>
        string(5) "test2"
    }
    ["heading2"]=>
    object(Config)#3 (2) {
        ["config_filename:private"]=>
        string(21) "../include/config.ini"
        ["str1"]=>
        string(9) "testagain"
    }
}

I would have preferred not to have recursively duplicated the ["config_filename:private"] property like it did. But I couldn't conceive a way around it. So if you know of a workaround then I'd appreciate a comment.

Thanks for all the help to get me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮华 2024-12-21 06:55:52

为什么它返回一个数组而不是一个对象[...]

我看到设置$this->_ini_array的唯一内容是parse_ini_file的返回值,它返回一个数组(数组的数组)。

如何返回一个对象?

您需要迭代相关数组并手动填充对象。

Why does this return an array instead of an object[...]

The only thing I see setting $this->_ini_array is the return value of parse_ini_file, which returns an array (an array of arrays).

how can I return an object?

You'll need to iterate over the pertinent array and populate an object manually.

眼眸里的快感 2024-12-21 06:55:52

根据您的代码,您正在解析一个 ini 文件,然后返回结果数组的一个元素。根据您的调用代码,您期望使用“db_pgsql”作为键返回一些值,该值将是一个字符串。

如果你想要一个对象,你必须实例化一个对象然后返回它,如下所示:

class Bar
{
}

class Foo
{

    public function __get($param)
    {
        return new $param();
    }
}

$foo = new Foo();
var_dump($foo->Bar);

According to your code you are parsing an ini file, then returning an element of the resulting array. According to your calling code you are expecting some value to be returned using 'db_pgsql' as the key, which will be a string.

If you want an object you will have to instantiate an object then return it, something like:

class Bar
{
}

class Foo
{

    public function __get($param)
    {
        return new $param();
    }
}

$foo = new Foo();
var_dump($foo->Bar);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文