Reply to comment

PHP const vs. static

Und noch ein kleiner Artikel in den frühen Morgenstunden...

Neulich kam mir mal wieder die Frage in den Kopf... "const" oder "static" nutzen für die Definition beispielsweise von Schlüsselwerten.

Kleines Beispiel:

Per const:

  1. class Person {
  2.     const KEY_VORNAME = 'vorname';
  3.     const KEY_NACHNAME = 'nachname';
  4.  
  5.     [...]
  6.  
  7.     public function toArray(){
  8.         return array(
  9.             self::KEY_VORNAME  => $this->...,
  10.             self::KEY_NACHNAME => $this->
  11.         );
  12.     }
  13. }

Per static:

  1. class Person {
  2.     protected static $keyVorname = 'vorname';
  3.     protected static $keyNachname = 'nachname';
  4.  
  5.     [...]
  6.  
  7.     public function toArray(){
  8.         return array(
  9.             self::$keyVorname  => $this->...,
  10.             self::$keyNachname => $this->
  11.         );
  12.     }
  13. }

Unterschiede zwischen den beiden Varianten sind die folgenden:

  • keine Veränderbarkeit der Sichtbarkeit bei const (eher nachteilig). Bei static über statische getter und setter fein einschränkbar
  • keine Veränderbarkeit des Wertes von const während der Laufzeit
  • mögliche ungewollte Manipulierbarkeit des Wertes bei static

Das fiel mir zunächst dazu ein...

Ich bin für mich selbst zu dem Schluss gekommen, dass Werte, die definitiv nie während der Laufzeit verändert werden sollten (wie beispielsweise die Schlüssel im Beispiel) mit einem const besser aufgehoben sind. Außerdem fällt die Schreibweise dann im Code auch besser auf.

Zwar geht es nicht darum, den Code vor dem Programmierer zu schützen, aber auch wenn man die Schlüssel dann außerhalb benutzt, wird deren unveränderbarer Charakter klarer.

Statische Attribute werde ich in Zukunft auch weiterhin für Eigenschaften der Klasse verwenden, die sich in Ausnahmefällen ändern dürfen oder beispielsweise fürs Singleton Pattern. Hier läge man mit const total falsch.

Interessant wäre hier noch eine bessere Handhabung von final wie in Java, dann könnte man sich das const tatsächlich komplett sparen.

Vielleicht gibt es ja sogar noch bessere Lösungen?

Einordnung:

Reply

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <smarty>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Textual smileys will be replaced with graphical ones.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.