# using $this-> implicitly inside same class

- Thread: https://externals.io/message/21996
- Original archive: https://news-web.php.net/php.internals/21996
- Started by php@karsites.net on 2006-02-24T14:05:11+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21996 — php@karsites.net — 2006-02-24T14:05:11+00:00

URL: https://externals.io/message/21996#21996

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)

<?php

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:

<?php

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.

---

## #21998 — Marcus Boerger — 2006-02-24T14:23:07+00:00

URL: https://externals.io/message/21996#21998 · In reply to: #21996

Hello php,

Friday, February 24, 2006, 3:05:11 PM, you 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.

While we could do this we decided against this years ago for a good reason.
It is faster the way we do it and the code is clearer and much easier
toread. Also introducing this now would be a major BC break.

> This would save ALOT of repetitive typing, and make the code
> alot more concise.

WOW, do your fingers hurt when typing "$this->" - wow 7 strokes.

> this.use_implicit could default to FALSE, so it would not 
> interfere with current code compatibility.

No chance for an ini option because that would make writing protable
code impossible.

[...]


Best regards,
 Marcus

---

## #22000 — Christian Schneider — 2006-02-24T14:55:56+00:00

URL: https://externals.io/message/21996#22000 · In reply to: #21996

php@karsites.net wrote:
> 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.

This would turn all local variables into global ones in regards to an 
object instance. One of the big benefits of PHP is that the default for 
variables is that they are local, you never have side-effects unless you 
specify it, i.e. you can safely introduce local variables without 
clashing with properties or global variables. Without $this-> you'd have 
to either ensure manually that you never have name clashes or introduce 
a declaration for local variables a la "my $var" in Perl. Neither is 
desirable. Ruby chose to replace "this->" by "@" but the basic concept 
is the same as in PHP.

Bottom-line: While you're proposal seems to improve the simple case it 
would introduce a big draw-back for everything a bit more complex, i.e. 
where you have temporary local variables - which isn't _that_ far 
fetched :-).

- Chris

---

## #22004 — Mike Lively — 2006-02-24T16:12:28+00:00

URL: https://externals.io/message/21996#22004 · In reply to: #21996

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)



> 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)
> 
> <?php
> 
> 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:
> 
> <?php
> 
> 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.

---

## #22008 — php@karsites.net — 2006-02-24T21:52:59+00:00

URL: https://externals.io/message/21996#22008 · In reply to: #21998

Thanks to Marcus and everyone else for their comments.

I note the valid points you raised.

Keith




> To: php@karsites.net
> From: Marcus Boerger <helly@php.net>
> Subject: Re: [PHP-DEV] using $this-> implicitly inside same class
> 
> Hello php,
> 
> Friday, February 24, 2006, 3:05:11 PM, you wrote:
> 
> > […]
> 
> > […]
> 
> > […]
> 
> While we could do this we decided against this years ago for a good reason.
> It is faster the way we do it and the code is clearer and much easier
> toread. Also introducing this now would be a major BC break.
> 
> > […]
> 
> WOW, do your fingers hurt when typing "$this->" - wow 7 strokes.
> 
> > […]
> 
> No chance for an ini option because that would make writing protable
> code impossible.
> 
> [...]
> 
> 
> Best regards,
>  Marcus

---

## #22010 — Xuefer — 2006-02-25T02:46:25+00:00

URL: https://externals.io/message/21996#22010 · In reply to: #21996

>
>
> I was thinking of something more like this:
>
> <?php
>
> class Connection {
>
declare (implicit_this_properties) {
>    // 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)
>    {
$key = 'server';
${$key} = $server_ip
>        $server    = $server_p;
now which is the $server? $this->server or local scope $server?
is they any side effect this "feature" make?
>        $username  = $username_p;
>        $password  = $password_p;
>        $db        = $db_p;
>        connect();
>    }
>
good idea but it's already proposed and rejected because php groups
don't like to make things complex.
