Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:21996 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68425 invoked by uid 1010); 24 Feb 2006 14:13:53 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 68409 invoked from network); 24 Feb 2006 14:13:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Feb 2006 14:13:53 -0000 X-Host-Fingerprint: 82.153.252.23 mra01.ch.as12513.net Linux 2.4/2.6 Received: from ([82.153.252.23:34362] helo=mra01.ch.as12513.net) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id FE/7F-30574-0A41FF34 for ; Fri, 24 Feb 2006 09:13:52 -0500 Received: from localhost (localhost [127.0.0.1]) by mra01.ch.as12513.net (Postfix) with ESMTP id E954C28C784 for ; Fri, 24 Feb 2006 14:13:05 +0000 (GMT) Received: from mra01.ch.as12513.net ([127.0.0.1]) by localhost (mra01.ch.as12513.net [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 32659-01-52 for ; Fri, 24 Feb 2006 14:13:05 +0000 (GMT) Received: from karsites.net (unknown [81.168.74.150]) by mra01.ch.as12513.net (Postfix) with ESMTP id D6EA628C74F for ; Fri, 24 Feb 2006 14:13:04 +0000 (GMT) Date: Fri, 24 Feb 2006 14:05:11 +0000 (GMT) X-X-Sender: keith@karsites.net To: internals@lists.php.net Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by Eclipse VIRUSshield at eclipse.net.uk Subject: using $this-> implicitly inside same class From: php@karsites.net 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) 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: 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.