Re: tired of referencing static variables/constants with 'self ::'

From: Date: Wed, 10 May 2006 00:29:02 +0000
Subject: Re: tired of referencing static variables/constants with 'self ::'
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-235835@lists.php.net to get a copy of this message
D. Dante Lorenso wrote:
John Wells wrote:
On 5/9/06, D. Dante Lorenso <dante@vocalspace.com> wrote:
Does anyone know if it's possible to reference class constants or static variables without having to use 'self::' all the time?
No, ... Why? The reason is SCOPE.
implicit scope sucks - it means you have check where the var/cnst/whatever is actually coming from when reading the code (and if there is a scope chain to follow then it is not always clear). - this goes against the php KISS philosphy - as long as you are writing php you will be specifying scope explicitly. I suggest reading up on why the 'with' syntax in javascript is considered evil(tm) by most people.
As wonderful as PHP is, it can't read your mind. So if you're looking for a constant or variable, PHP needs to know where to look. If you don't specify "self::', PHP will look in the global scope, as it did in your code example. If you want it to look within your classes, you need to *tell* it so.
As an OOP programmer, I would expect the scope search to be as follows: 1. LOCAL: current method 2. THIS: current instance ($this) 3. SELF: current class & parent classes, in order of inheritance 4. GLOBAL: globals I really hate globals because it defeats my style of encapsulation. If the search for my constant follows the search I've listed above, self would never be necessary unless you wanted to pinpoint "3" directly. Under this same line of thinking, though, '$this->' really shouldn't be be necessary either unless you wanted to clarify local vs this for same-named variables.
And is "self::" really a lot of boiler-plate? Really??
YES. $x = ($y == self :: MY_CONSTANT || $y == self :: MY_CONSTANT2);
I hate the spaces around the '::' whynot self::MY_CONSTANT?
vs $x = ($y == MY_CONSTANT || $y == MY_CONSTANT2); My philosophy is that the more code which is written, the greater the probability of bugs. Writing less code means less to read and less to read means easier to comprehend.
that is such an oversimplication is almost laughable - comprenhesibility is not at all directly bound to the number of characters used to write something. if you have ever read up on how reading/comprehension works in the human brain you should know that - 'self::' is 'read' as a single 'token' once the brain is acustomed to recognizing it.
From what I can tell, PHP only uses 1) LOCAL and 4) GLOBAL scope unless you specifically use $this or self, but I just don't think those should be necessary. Other than "that's just the way it is", why IS it necessary to use $this and self?
it's a consciencious design decision - and a very good one at that. (as apposed to say not making 'self' bound at runtime from the very beginning of php5's inception [or atleast offering a sibling token to 'self' that is bound at runtime] - but don't get me started on that!)
Dante
...

« previous php.general (#235835) next »