- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我一直在寻找一种方法来实现 ArrayObject 类来存储应用程序配置,我在 php 手册(评论之一)中找到了这个实现
<?php
use \ArrayObject;
/**
* Singleton With Configuration Info
*/
class Config extends ArrayObject
{
/**
*
* Overwrites the ArrayObject Constructor for
* Iteration throught the "Array". When the item
* is an array, it creates another static() instead of an array
*/
public function __construct(Array $array)
{
$this->setFlags(ArrayObject::ARRAY_AS_PROPS);
foreach($array as $key => $value)
{
if(is_array($value))
{
$value = new static($value);
}
$this->offsetSet($key, $value);
}
}
public function __get($key)
{
return $this->offsetGet($key);
}
public function __set($key, $value)
{
$this->offsetSet($key, $value);
}
/**
* Returns Array when printed (like "echo array();")
* Instead of an Error
*/
public function __ToString()
{
return 'Array';
}
}
用法:
$config = new Config\Config($settings);
$config->uri = 'localhost'; // works
$config->url->uri = 'localhost'; // doesn't work
print_r($config);
我已经尝试将 __get 和 __set 添加到此类中,这对于简单数组来说效果很好,但是当涉及到多维数组时……情况就不同了。我收到一条错误消息,指出未定义索引。有人可以帮助我吗?我尝试了所有我知道的东西,在谷歌上搜索了很多,但我没有找到解决方案。
我已经解决了这个类的问题。稍后我会在这里发布一个完整的工作示例,也许有人会需要它。感谢大家花时间阅读这篇文章
更新:那么你们怎么看呢?我应该做哪些改进...更改?
public function __construct(Array $properties)
{
$this->populateArray($properties);
}
private function populateArray(Array $array)
{
if(is_array($array))
{
foreach($array as $key => $value)
{
$this->createProperty($key, $value);
}
}
unset($this->properties);
}
private function createProperty($key, $value)
{
is_array($value) ?
$this->offsetSet($key, $this->createComplexProperty($value))
: $this->offsetSet($key, $value);
}
private function createComplexProperty(Array $array)
{
return new Config($array);
}
private function createPropertyIfNone($key)
{
if($this->offsetExists($key))
return;
$this->createProperty($name, array());
}
public function __get($key)
{
$this->createPropertyIfNone($key);
return $this->offsetGet($key);
}
public function __set($key, $value)
{
$this->createProperty($key, $value);
}
public function __ToString()
{
return (string) $value;
}
}
最佳答案
如果您想假设一个不存在的键是一个数组,那么这应该可行。
public function __get($key)
{
if(!$this->offsetExists($key))
{
$this->offsetSet($key,new Array());
}
return &$this->offsetGet($key);
}
用法:
$config = new Config\Config($settings);
$config->url['uri'] = 'localhost';
print_r($config);
编辑:
不确定是否必须返回引用才能使它起作用。
return &$this->offsetGet($key);
或者这个
return $this->offsetGet($key);
关于php - 使用 ArrayObject 的配置类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944599/
我试图理解文档中的 STD_PROP_LIST 常量,但到目前为止我不明白,也没有找到任何解释:(该文档有以下示例: $a = new ArrayObject(array(), ArrayObject
如果是,那么它与使用 arrayObject = [] 有何不同.这是两件事不同还是两种做同样的事情。 最佳答案 array.length = 0; 保留对原始数组的对象引用。 array = [];
我收到了这条通知: ArrayIterator::next(): Array was modified outside object and internal position is no longe
我想了解什么是getIterator(),我会解释一下: 据我所知,getIterator 是我们调用以包含外部迭代器的方法。 问题是 getIterator 包括它自己的方法 关闭认为看起来与 It
问题: 当手动设置或读取值时,ArrayObject 会按预期工作,但是当使用函数(例如 foreach)对其进行迭代时,事情会变得很糟糕。 它不调用我定义的 offset* 方法,而是使用 Arra
我在哪里可以找到 ArrayObject 的完整源代码(PHP 格式)? 我不明白的是为什么在向 ArrayObject 添加元素时可以使用“箭头”,例如: $a = new ArrayObject(
应该使用哪一个来操作数据,数组还是数组对象?像搜索、排序和其他数组操作。 最佳答案 基本类型是array。这是可以在循环中写入、读取和访问的键和值的映射。 ArrayObject 是一个类,您可以扩展
我正在尝试存储一个数组并使用扩展 ArrayObject 的自定义类来操作该数组。 class MyArrayObject extends ArrayObject { protected $d
我编写了一个使用 Class::ArrayObjects 的简单程序,但它没有按预期工作。程序是: TestArrayObject.pm: package TestArrayObject; use C
ideone 示例代码: array('d')); print_r($a); unset($a['b']['c']); print_r($a); 输出 ArrayObject Object (
我一直在寻找一种方法来实现 ArrayObject 类来存储应用程序配置,我在 php 手册(评论之一)中找到了这个实现 setFlags(ArrayObject::ARRAY_AS_PROPS);
我正在尝试访问公共(public)函数 get_data(),以生成“此处”的输出,以查看是否从对象数组创建动态对象..那么我该怎么做。 #include #include using namesp
我正在做 FRVT 1:1 验证。所以我需要使用FRVT提供的程序。我已经连接到我编写的程序,并完成了实现。 但是我想把我用cython写的东西一步步移植到NullImp FRVT 提供的示例。但我得
这个问题已经有答案了: Type safety: Unchecked cast (10 个回答) 已关闭 7 年前。 我该如何解决这个警告? Type safety: Unchecked cast f
如标题所述,如何在 arrayObject 上执行 array_unshift(),array_push() 是通过执行arrayObject->append() 但是 unshift 呢? 编辑:我
问题:我正在尝试扩展 PHP 的 ArrayObject,如下所示。不幸的是,在设置多维对象时我无法让它正常工作,而是抛出了一个错误,因为我在 PHP 中启用了严格设置。 (错误:严格标准:从空值创建
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在 ArrayObject 中寻找 __destroy() 方法,但没有找到实现。如果我将包含 ArrayObject 的变量设置为 NULL,它会正确地销毁存储在其中的所有对象并释放内存吗?或者我
我不知道如何使用 SwiftyJSON.JSON 制作数组我可以使用 SwiftyJSON.JSON 解析结构,但无法解析对象数组。请帮助我。 JSON { "result": { "li
我发现很多类似的问题,人们对这个文件有疑问。我知道头文件在哪里,我用 -I 解决 g++,因为人们通常会回答前面的问题。我做了所有这些,但他们没有工作。 1 , 2 sum_array_wrap.cp
我是一名优秀的程序员,十分优秀!