anewt-developers team mailing list archive
-
anewt-developers team
-
Mailing list archive
-
Message #00238
[Branch ~uws/anewt/anewt.uws] Rev 1769: [core] Allow non-magic container property access
------------------------------------------------------------
revno: 1769
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Sat 2010-03-06 14:58:45 +0100
message:
[core] Allow non-magic container property access
Using $x->foo was the same as $x->get('foo') already, and
with this change $x->_foo is the same as $x->_get('foo').
Also added test code for container property access.
modified:
core/container.lib.php
core/container.test.php
--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws
Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'core/container.lib.php'
--- core/container.lib.php 2010-01-05 20:21:31 +0000
+++ core/container.lib.php 2010-03-06 13:58:45 +0000
@@ -108,7 +108,10 @@
*/
function __get($name)
{
- return $this->get($name);
+ if ($name{0} === '_')
+ return $this->_get(substr($name, 1));
+ else
+ return $this->get($name);
}
/**
@@ -174,7 +177,10 @@
*/
function __set($name, $value)
{
- $this->set($name, $value);
+ if ($name{0} === '_')
+ $this->_set(substr($name, 1), $value);
+ else
+ $this->set($name, $value);
}
/**
@@ -307,7 +313,10 @@
*/
function __isset($name)
{
- return $this->is_set($name);
+ if ($name{0} === '_')
+ return $this->_isset(substr($name, 1));
+ else
+ return $this->is_set($name);
}
/**
=== modified file 'core/container.test.php'
--- core/container.test.php 2009-08-02 16:32:09 +0000
+++ core/container.test.php 2010-03-06 13:58:45 +0000
@@ -113,4 +113,29 @@
assert('$c->_getdefault("bar") === "anothervalue"');
assert('$c->_getdefault("this-one-is-not-set", "the-answer") === "the-answer"');
+
+/* Test property access */
+
+$c = new AnewtContainer();
+assert('isset($c->foo) === false');
+$c->set('foo', 'bar');
+assert('isset($c->foo) === true');
+assert('$c->foo === "bar"');
+unset($c->foo);
+assert('isset($c->foo) === false');
+$c->foo = 'baz';
+assert('$c->foo === "baz"');
+assert('$c->_get("foo") === "baz"');
+
+$t = new TestContainer();
+assert('$t->is_set("foo") === true');
+assert('$t->_isset("foo") === false');
+assert('isset($t->foo) === true');
+assert('isset($t->_foo) === false');
+
+$t->_foo = '_foo';
+assert('isset($t->_foo) === true');
+assert('$t->foo === "foo_value"');
+assert('$t->_foo === "_foo"');
+
?>