Hi!

> Within get: $this->Hours can read the underlying property but not write 
> to it, if it attempts to write, that write would go through the setter.
> Within set: $this->Hours = 1 can write to the underlying property but a 
> read of the property would go through the getter.

Are the accesses also applying to called functions/accessors? I.e.
consider this:

class SuperDate {
        private $date {
                get;
                set(DateTime $x) { $this->date = $x; $this->timestamp =
$x->getTimestamp();
        }
        private $timestamp {
                get;
                set($t) { $t = (int)$t; $this->timestamp = $t; $this->date = new
DateTime("@$t"); }
        }
}

What happens to it? Would it get into infinite loop or will just set the
value twice? What would be the correct way to write such a code (note
the real code of course could be much more complicated and probably
involve dozen of properties with complex dependencies between them).

Also, if this applies to functions called from getter/setter (which
seems to be the case from the code, unless I miss something), consider this:

class UserContext {
        protected $user;
        public $logger;
        public $username {
                get() { $this->logger->log("Getting username"); return 
$user->name; }
                set($n) { $this->user = User::get_by_name($n); }
        }
}

class Logger {
        protected $ctx;
        public function __construct(UserContext $ctx) {
                $this->ctx = $ctx;
                $this->logfile = fopen("/tmp/log", "a+");
        }
        public function log($message) {
                fwrite($this->logfile, "[$this->ctx->username] $message\n");
        }
}

$u = new UserContext();
$u->logger = new Logger($u);
$u->username = "johndoe";
echo $u->username;

What would happen with this code? Will the log be able to log the actual
user name, and if not, how you protect from such thing? $username is a
part of public API of UserContext, so whoever is writing Logger has
right to use it. On the other hand, whoever is using logger->log in
UserContext has absolutely no way to know that Logger is using
ctx->username internally, as these components can change completely
independently and don't know anything about each other besides public APIs.
What I am getting at here is that shadowing seems to create very tricky
hidden state that can lead to very bad error situations when using
public APIs without knowledge of internal implementation.

> Within isset/unset: the same rules apply, a read goes through the getter 
> and a write goes through the setter.

With this code:

class Foo {
        public $bar {
                get;
                set;
        }
}

How could I make it set to 2 by default and isset() return true when I
instantiate the class? Currently, I see no way to assign default values
for properties. Is it planned?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to