Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64501 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30838 invoked from network); 3 Jan 2013 14:21:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jan 2013 14:21:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=steve@mrclay.org; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=steve@mrclay.org; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mrclay.org from 50.22.11.19 cause and error) X-PHP-List-Original-Sender: steve@mrclay.org X-Host-Fingerprint: 50.22.11.19 bedford.accountservergroup.com Linux 2.6 Received: from [50.22.11.19] ([50.22.11.19:38286] helo=bedford.accountservergroup.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 61/E7-35624-3E395E05 for ; Thu, 03 Jan 2013 09:21:24 -0500 Received: from n128-227-98-14.xlate.ufl.edu ([128.227.98.14]:50270 helo=Distance-Ed-Sclay.local) by bedford.accountservergroup.com with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.80) (envelope-from ) id 1Tqlfb-000AU9-Rw for internals@lists.php.net; Thu, 03 Jan 2013 08:21:20 -0600 Message-ID: <50E593DF.9090004@mrclay.org> Date: Thu, 03 Jan 2013 09:21:19 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: internals@lists.php.net References: <50E41BB6.4030901@zerocue.com> <50E4A43E.6030302@zerocue.com> <50E4B232.5000505@mrclay.org> <50E4BDDE.8050509@zerocue.com> <50E4D0BB.7060701@mrclay.org> <50E4FA09.7030001@zerocue.com> <50E529B6.1050903@sugarcrm.com> In-Reply-To: <50E529B6.1050903@sugarcrm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bedford.accountservergroup.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - mrclay.org Subject: Re: [PHP-DEV] [PHP-RFC] Property Accessors 1.2 for Final Review before Vote From: steve@mrclay.org (Steve Clay) On 1/3/13 1:48 AM, Stas Malyshev wrote: > 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 I think infinite recursion is a potential issue for lots of logging setups ("let's log when someone calls the logger!") and situations where you have multiple values to keep in sync. The accessor implementation shouldn't try to solve these design problems. > 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. Again, the problem is not shadowing (not even in use here) but really general information hiding. You can create these problems anytime you have hidden information and interdependent objects, and it's an API design problem. Steve Clay -- http://www.mrclay.org/