Chris wrote:
While playing with an unnamed framework, I think I discovered an
overloading limitation (PHP 5.1.2). Can someone please confirm this
limitation?
Example class:
class testClass
{
public $vars = array();
public function __get($key)
{
return array_key_exists($key, $this->vars) ? $this->vars [$key]
: null;
}
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
public function __isset($key)
{
return array_key_exists($key, $this->vars);
}
public function __unset($key)
{
unset($this->vars[$key]);
}
}
Given the above class, the following code will not work:
$tc = new testClass();
$tc->arr = array();
$tc->arr['a'] = 'A';
$tc->arr['b'] = 'B';
if (isset($tc->arr['b'])) {
unset($tc->arr['b']);
}
//var_dump is only to see results of above
var_dump($tc);
Am I a moron or, in fact, does this not work and is a language limitation?
I think its a misunderstanding on the one side and a limitation on the other,
you can't use overloading directly on items of an overloaded array e.g:
echo $tc->arr['a']
this is triggers a call to __get() with the $key parameter set to something like
(I'm guessing) "arr['a']" ... what $key is set to it surely wont be a key in the
array testClass->vars (do some test otherwise to see eactly what $key
contains if you do "echo $tc->arr['c']" maybe it contains something useful after all)
try this:
<?php
$tc = new testClass();
$tc->a = 'A';
$tc->b = 'B';
if (isset($tc->b)) {
unset($tc->b);
}
var_dump($tc);
Chris
chris@xxxxxxxxxxxxxxxxx
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php