php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48278 $ variable prefix is unneeded in most cases
Submitted: 2009-05-14 09:04 UTC Modified: 2009-05-14 22:16 UTC
From: zyss at mail dot zp dot ua Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.9 OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: zyss at mail dot zp dot ua
New email:
PHP Version: OS:

 

 [2009-05-14 09:04 UTC] zyss at mail dot zp dot ua
Description:
------------
$ character is one of the most widely used in PHP code, in most cases its use is unreasonable. I understand its historical roots, it came from Unix shell scripts (such as bash) where it was required to make interpreter?s "life" easier. But today's most powerful languages do not require use of variable prefix to distinguish between variable and other identifiers.

There are some cases where use of $ if reasonable, for example inside strings and when calling function by name stored in a variable. In all other cases programmers are dazzled by it.

I do not propose to remove $ prefix, it would be a major improvement to make it OPTIONAL (this change will not break any existing code but many people will breathe a sigh of relief).

P.S. If you will continue to prefer PHP-interpreter developers' convenience over PHP-code developers' we will start our own fork of PHP to be able to add required functionality to it.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-14 11:07 UTC] scottmac@php.net
Fork away, it is open source after all.


 [2009-05-14 18:24 UTC] rasmus@php.net
And it will most definitely break stuff since removing the $ would clash with any defined constants.
 [2009-05-14 19:43 UTC] zyss at mail dot zp dot ua
It will not break stuff if defined constants will have higher priority. In such case developer can just leave $ prefix.

It would be really great if PHP developers have an option to choose whether to use the prefix or not. Eventually all new code will be written without $ and there will be no conflicts.
 [2009-05-14 19:59 UTC] rasmus@php.net
There are also edge cases like 

$a['foo'] = 1;
$foo = 2;
echo $a[foo];

That would behave differently with optional $'s

And I like the obvious separation between constants and variables. It avoids side-effects if someone somewhere suddenly creates a constant that then goes and changes behaviour all over the place because of this constants-over-variables approach.

 [2009-05-14 22:01 UTC] zyss at mail dot zp dot ua
It is a matter of programmer's personal attentiveness. In your example there is a mix of using $ and not using it. Most developers will not do it. Besides in other languages this theoretical problem is even not discussed. For example, as you know in C/C++ there is a de facto standard of using CAPITAL LETTERS for constants which is enough to distinguish them from variables. Many PHP developers follow this concept.

From the other side if one wanted to calculate how many key strokes will it save to allow not to use $ prefix in a PHP project with 100000 lines of code - this number would be huge. No one will argue that PHP is derived from C. It is evident that C language itself was created to minimize typing (as one of its primary goals, comparing to academic Pascal is enough to see this). So why not to follow the original path - to avoid unneeded typing while leaving a possibility to use $ for compatibility and in some special cases?

The same arguments can be applied to { } arrays and "in" operator.
 [2009-05-14 22:06 UTC] rasmus@php.net
Minimizing keystrokes never has and never will be a priority.
 [2009-05-14 22:16 UTC] zyss at mail dot zp dot ua
It is just one of arguments, another one is that it will improve code readability. While it is not so evident in simple examples: who cares what to write

  $foo = $bar

or

  foo = bar

and all of these seems to be clear and readable. But consider more complicated code:

  while (TRUE) {
    $k = strpos( $sql, $quoteChar, $j );
    $escaped = false;
    if ($k === false) {
      break;
    }
    $l = $k - 1;
    while ($l >= 0 && $sql{$l} == '\\') {
      $l--;
      $escaped = !$escaped;
    }
    if ($escaped) {
      $j = $k + 1;
      continue;
    }
    break;
  } 

Isn't it more readable without $:

  while (TRUE) {
    k = strpos( sql, quoteChar, j );
    escaped = false;
    if (k === false) {
      break;
    }
    l = k - 1;
    while (l >= 0 && sql{l} == '\\') {
      l--;
      escaped = !escaped;
    }
    if (escaped) {
      j = k + 1;
      continue;
    }
    break;
  } 

?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Oct 12 09:01:27 2024 UTC