具有__set和__get的类中的多维数组 [英] Multidimensional arrays in class with __set and __get
问题描述
我正在尝试编写一个包装$ _SESSION的简单类.这个想法是,我将能够从类中获取并引用$ _SESSION变量的值,并让__set和__get执行我需要的其他东西.
I'm trying to write a simple class that wrap $_SESSION. The idea is that I'll be able to set and get values from the class that will refer to the $_SESSION variable, having the __set and __get execute some other stuff I need.
我目前拥有的东西,还远远不能完成:
What I currently have, which is nowhere near complete:
class Session
{
// Property declarations
public $data = array();
/**
* Class initialization
*/
public function __construct()
{
if (!session_id()) {
ini_set('session.cookie_lifetime', 0);
ini_set('session.gc-maxlifetime', 0);
session_start();
}
$this->data =& $_SESSION;
}
/**
* Get session ID
* @return string
*/
public function getId()
{
return session_id();
}
}
我正在尝试使其工作如下:
I'm trying to make it work as follows:
$session->images['directory'] = 1;
将与
$_SESSION['images']['directory'] = 1;
和
echo $session->images['directory'];
应依次输出:1
问题是,我想使用多维数组,如下所示:
$session->images['foo']['bar'] = 'works';
在SO和php.net的许多示例中都提供了常规的__set和__get函数,这是行不通的.
With regular __set and __get functions provided in many examples around SO and php.net, this doesn't work.
当然,在页面重新加载/导航后,我将能够使用它们各自的值访问相同的变量.
Ofcourse, after a page reload/navigation, I'll be able to access the same variables with their respective values.
我尝试过的搜索是Google和Stackoverflow,我一直使用"ArrayAcces",但是似乎无法弄清楚如何将其用于我的问题.
What I've tried is search Google and Stackoverflow, I keep landing on "ArrayAcces", but cannot seem to figure out how to use that with my problem.
有人指出我正确的方向吗?
Anyone point me in the right direction?
推荐答案
我知道这里已经存在了一段时间-但这是您的答案.
I know this has been here for quite a while - but here is your answer.
最初,我认为您必须使用递归.但是后来我想起了PHP做一个引用指针.因此,我去了PHP在线文档中的信息:
Originally I thought you had to use recursion. But then I remembered that PHP does a reference pointer. So I went and looked up the information in the PHP online documentation at:
http://php.net/manual/en/language.references.php
这导致我尝试了一些尝试,然后我想到了以下内容.现在,我使用了in()和out(),因为我没有使用__set和__get对此进行测试.我只是想让它工作.所以这是两个函数:
This led me to try some things out and I came up with the following. Now, I used in() and out() because I wasn't testing this with __set and __get. I just wanted it to work. So here are the two functions:
public function in( $v, $d )
{
$v = $this->get_id( $v );
$a = explode( ".", $v );
$b = &$this->info;
foreach( $a as $k=>$v ){
if( !isset($b[$v]) ){ $b[$v] = array(); $b = &$b[$v]; }
else { $b = &$b[$v]; }
}
$b = $d;
return true;
}
public function out( $v )
{
$v = $this->get_id( $v );
$a = explode( ".", $v );
$b = &$this->info;
foreach( $a as $k=>$v ){
if( isset($b[$v]) ){ $b = &$b[$v]; }
else { return false; }
}
return $b;
}
我用in("this.is.a.test",5)和out("this.is.a.test")测试了上面的代码.产生了以下输出:
I tested the above code with in("this.is.a.test", 5) and out("this.is.a.test"). Which produced the following output:
In: 1
Out: 5
Array
(
[cleric] => Array
(
[this] => Array
(
[is] => Array
(
[a] => Array
(
[test] => 5
)
)
)
)
)
(是的-我正在研究一个D& D程序,以便为我跟踪字符.)
(Yes - I'm working on a D&D program to keep track of characters for me.)
我在执行IN和OUT功能的上方设置了牧师"前缀.因此,第一行(牧师")来自前缀.:-)
I had set a prefix of "cleric" above where I did the IN and OUT functions. So the first line (the "cleric") is from the prefix. :-)
无论如何,这比使用递归执行此操作要快得多.我希望这有帮助.:-)或者,如果您已经从CakePHP中得到答案,那就太好了!:-)
Anyway, this is a LOT faster than using recursion to do this. I hope this helps. :-) Or if you've already got your answer out of CakePHP - that's great too! :-)
这篇关于具有__set和__get的类中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!