13

Is it safe to have single letter class names like A, B, P, etc?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 7
    Single letter class names are *boring*. Here is where it gets interesting: `class Ü { }`... In theory, even a class named with the Unicode `HOT BEVERAGE` character is possible: http://www.fileformat.info/info/unicode/char/2615/index.htm It works in PHP 5.3, but unfortunately, no programming font seems to support it. :) – Pekka Aug 05 '10 at 15:56
  • Sure it can? I thought that apart from alphanumeric + `_` only `\x7f-\xff` were allowed? And hot beverage is above that, I think. – NikiC Aug 05 '10 at 15:59
  • 6
    Strange! `class ☂ { }` works on PHP 5.3. (For those missing the right font, that is `\ux2602 UMBRELLA` – Pekka Aug 05 '10 at 15:59
  • @nikic yeah, I thought that too! Strange. – Pekka Aug 05 '10 at 16:00
  • Someone ought to tell them they update the documentation at http://www.php.net/manual/en/functions.user-defined.php, if PHP 5.3 really allowes that cool kind of stuff. – NikiC Aug 05 '10 at 16:01
  • @nikic I can't say for 100% certain yet. It could be that the umbrella and the hot beverage get somehow transliterated by my IDE (it shows a box instead). Interesting, I'll take a look into it (or maybe ask a question) when I get around to it. – Pekka Aug 05 '10 at 16:02
  • `function ÜÄÖß京警谨鲸Αυτουрусски() { return 1; }` works in PHP – Gordon Aug 05 '10 at 16:05
  • 2
    @Gordon Just joking around :) Although if `UMBRELLA` works, it will become an easter egg in one of my apps. `$umbrella = new ☂();` is too good *not* to do. Or `☠();` (`SKULL AND CROSSBONES`) as a synonym for `unset()`. The *possibilities!* – Pekka Aug 05 '10 at 16:06
  • 8
    Actually, I'd use ☠(); for die(); and OMG it works #lol – Gordon Aug 05 '10 at 16:07
  • 2
    And I'm gonna `define('π', pi());` – Gordon Aug 05 '10 at 16:17
  • I don't get why this works. Zend defines `{LABEL}` still same as in docs. But maybe I simply don't get all this yy stuff. – NikiC Aug 05 '10 at 16:30
  • @nikic apparently, the entire unicode range works `define('␀', NULL);` although the manual clearly says, it should be `[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*` – Gordon Aug 05 '10 at 16:44
  • Yes, and not only the manual says so, the code says so, too! I simply don't get it :( – NikiC Aug 05 '10 at 16:59
  • @nikic @Pekka made a question out of it http://stackoverflow.com/questions/3417180/unicode-class-names-bug-or-feature – Gordon Aug 05 '10 at 17:00
  • So what will the server admin think when he see's a file named ☠ for the class? – Xeoncross Jul 12 '11 at 21:48

7 Answers7

27

No.

Your future self will build a time machine for the sole purpose of slapping you for writing such unreadable code. And then, a paradox will result, and all of reality as we know it will be destroyed.

Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
11

Why should it be unsafe? You may use them, if you want. It only is discouraged, because single char class names aren't very descriptive...

Maybe you want to give the class a normal name but let the user import it with a short alias?

use Some\Long\But\Descriptive\Classname as A;
NikiC
  • 100,734
  • 37
  • 191
  • 225
4

Safe, yes. Readable, no.

Matt B
  • 8,315
  • 2
  • 44
  • 65
4

It's interpreter-safe. But it may not keep you "safe" from fellow programmers who have to support it :)

Out of curiosity, what would be the compelling reason for that? I assume there must be one.

David
  • 208,112
  • 36
  • 198
  • 279
  • They are representing html tags such as ``, `

    `, etc

    – Ali Aug 05 '10 at 16:15
  • Or BoldElement, ParagraphElement, etc. Maybe shorten them a bit, but the HTML tags themselves represent something to which the class names can be more descriptive. – David Aug 06 '10 at 01:21
1

There's no reason why it shouldn't be, but it's more useful to give your classes a meaningful name

EDIT

Link to PHP naming guide

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

Yeah there is nothing stopping you except for the confusion factor.

dewalla
  • 1,317
  • 8
  • 18
  • 42
1

only safe if it's a class that communicates with no other by the same name

bcosca
  • 17,371
  • 5
  • 40
  • 51