PHP __get 重载返回数组而不是对象
为什么它返回一个数组而不是一个对象以及如何返回一个对象?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到设置
$this->_ini_array
的唯一内容是parse_ini_file
的返回值,它返回一个数组(数组的数组)。您需要迭代相关数组并手动填充对象。
The only thing I see setting
$this->_ini_array
is the return value ofparse_ini_file
, which returns an array (an array of arrays).You'll need to iterate over the pertinent array and populate an object manually.
根据您的代码,您正在解析一个 ini 文件,然后返回结果数组的一个元素。根据您的调用代码,您期望使用“db_pgsql”作为键返回一些值,该值将是一个字符串。
如果你想要一个对象,你必须实例化一个对象然后返回它,如下所示:
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: