Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:22004 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40411 invoked by uid 1010); 24 Feb 2006 16:12:25 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 40396 invoked from network); 24 Feb 2006 16:12:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Feb 2006 16:12:25 -0000 X-Host-Fingerprint: 66.240.252.15 unknown Linux 2.5 (sometimes 2.4) (4) Received: from ([66.240.252.15:46566] helo=mail.ft11.net) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id FF/6A-30574-8603FF34 for ; Fri, 24 Feb 2006 11:12:24 -0500 Received: (qmail 22795 invoked by uid 510); 24 Feb 2006 08:12:20 -0800 Received: from 69.167.181.84 by fc425215.aspadmin.net (envelope-from , uid 508) with qmail-scanner-1.25-st-qms (clamdscan: 0.87.1/1209. spamassassin: 3.0.4. perlscan: 1.25-st-qms. Clear:RC:0(69.167.181.84):SA:0(0.2/5.0):. Processed in 1.240753 secs); 24 Feb 2006 16:12:20 -0000 X-Spam-Status: No, hits=0.2 required=5.0 X-Antivirus-MYDOMAIN-Mail-From: mlively@ft11.com via fc425215.aspadmin.net X-Antivirus-MYDOMAIN: 1.25-st-qms (Clear:RC:0(69.167.181.84):SA:0(0.2/5.0):. Processed in 1.240753 secs Process 22788) Received: from couerdalene-cuda2-69-167-181-84.losaca.adelphia.net (HELO ?192.168.1.101?) (mlively@ft11.net@69.167.181.84) by mail.ft11.net with SMTP; 24 Feb 2006 08:12:19 -0800 Reply-To: mlively@ft11.com To: internals@lists.php.net In-Reply-To: References: Content-Type: text/plain Organization: FT11 Interactive Date: Fri, 24 Feb 2006 08:12:28 -0800 Message-ID: <1140797549.3089.13.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.2.3 (2.2.3-2.fc4) Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] using $this-> implicitly inside same class From: mlively@ft11.com (Mike Lively) One of the things I have always really liked about php is that variable scope is always very explicit. You always know where a variable is coming from and you don't ever have to worry about where or not you are working with a variable in the right scope. If something like this were to ever creep into php I would be very sad. ---------- Mike Lively (ds- on irc.efnet.org) On Fri, 2006-02-24 at 14:05 +0000, php@karsites.net wrote: > Hi all. > > I'm using php 5.1.2 compiled from source, with Apache 2.2.0 > and MySQL 5.0.18 on SuSE Linux 9.2 pro. > > I really do not see the need to keep telling php I'm > refering to the properties and method of the class I'm > already in, when php 5 should be able to deduce this from > the context of the class I'm coding in. > > I was wondering if it would be possible in future versions > of php to provide a PHP_INI_CLASS constant and a > this.use_implicit TRUE|FALSE option please, valid only > within a class's definition? I guess it could be moved to > php.ini later, if it became popular, and was required there. > > This would allow a programmer to set the default value of > whether to use $this-> as a prefix to class properties and > methods, or not. (Similar to the way php 5.1.2 handles > methods without a visibility declaration - i.e. they default > to public) > > This option to select default implicit referencing could > also apply to inherited properties and methods. > > This would save ALOT of repetitive typing, and make the code > alot more concise. This would be similar to & being made > implicit when passing object references to method calls. > > this.use_implicit could default to FALSE, so it would not > interfere with current code compatibility. > > Developers could then choose to enable this.use_implicit > on a class by class basis. This would allow a developer to > do a gradual upgrade of their classes, without breaking any > code. > > The only problem I can see, is that the parameters passed to > the __construct() function would have to use different names > to avoid any ambiguity conflicts - see example below. > > As the parameters are only usually passed into a class once, > when the object is instantiated, this would cut down on a > lot of coding once the parameters are inside the class. > > So, instead of coding something like: > > (From chapter 19 example 25 of the manual) > > > class Connection { > protected $link; > private $server, $username, $password, $db; > > public function __construct($server, $username, $password, $db) > { > $this->server = $server; > $this->username = $username; > $this->password = $password; > $this->db = $db; > $this->connect(); > } > > private function connect() > { > $this->link = mysql_connect($this->server, $this->username, $this->password); > mysql_select_db($this->db, $this->link); > } > > public function __sleep() > { > mysql_close($this->link); > } > > public function __wakeup() > { > $this->connect(); > } > } > ?> > > > I was thinking of something more like this: > > > class Connection { > > // use $this-> implicitly within this class only > ini_set('this.use_implicit', ON); > > protected $link; > private $server, $username, $password, $db; > > public function __construct($server_p, $username_p, $password_p, $db_p) > { > $server = $server_p; > $username = $username_p; > $password = $password_p; > $db = $db_p; > connect(); > } > > private function connect() > { > $link = mysql_connect($server, $username, $password); > mysql_select_db($db, $link); > } > > public function __sleep() > { > mysql_close($link); > } > > public function __wakeup() > { > connect(); > } > } > > ?> > > > Any comments or suggestions would be welcomed. > > Keith Roberts > > In theory, theory and practice are the same; > In practice they are not. >