User Tools

Site Tools


en:dev:all:psr:psr-1

This is an old revision of the document!


FIXME This page is not fully translated, yet. Please help completing the translation.
(remove this paragraph once the translation is finished)

originating from PHP-FIG PSR-1

Basic Coding Standard

This section of the standard comprises what should be considered the standard coding elements that are required to ensure a high level of technical interoperability between shared PHP code.

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119.

Overview

  • Files MUST use only <?php (:!: adaption for WB: and <?=) tags.
  • Files MUST use only UTF-8 without BOM for PHP code.
  • Files SHOULD either declare symbols (classes, functions, constants, etc.) OR contain directly executabl code (cause side-effects), but SHOULD NOT do both.
  • Namespaces and classes MUST follow an “autoloading” PSR: PSR-0.
  • Class names MUST be declared in StudlyCaps.
  • Class constants MUST be declared in all upper case with underscore separators.
  • Method names MUST be declared in camelCase.

Files

PHP Tags

PHP code MUST use the long <?php ?> tags (:!: adaption for WB: or the short-echo <?= ?> tags); it MUST NOT use the other tag variations.

Character Encoding

PHP code MUST use only UTF-8 without BOM.

Side Effects

A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other 'side effects', or it SHOULD execute logic with 'side effects', but SHOULD NOT do both.

The phrase 'side effects' means 'execution of logic' not directly related to declaring classes, functions, constants, etc., merely from including the file. 'Side effects' include but are not limited to: generating output, explicit use of require() or include(), connecting to external services, modifying INI-settings, emitting errors or exceptions, modifying global or static variables, reading from or writing to a file or a database, and so on.
:!: Adaption for WB: Files with 'side effects' MUST be secured in a special manner so that they can not be called e.g. by DeepLinks and could cause any damage this way.

The following is an example of a file with both declarations and 'side effects'; i.e, an example of what to avoid:

<PHP> side effect:change ini settings ini_set('errot_reporting', E_ALL); side effect: lädt loads a file include('file.php');

side effect: generates output echo '<html>'.PHP_EOL; declaration function foo() {

  // function body

} </PHP> The following example is of a file that contains declarations without side effects; i.e., an example of what to emulate:

<PHP> declaration function foo() { function body }

conditional declaration is *not* a side effect if (! function_exists('bar')) { function bar() { function body

  }

} </PHP>

Namespaces und Klassennamen

Die Benennung von Namespaces und Klassen MUSS nach PSR-0 erfolgen.

Das bedeutet, dass jede Klasse in einer eigenen Datei ist und einem Namespace zugewiesen, der wenigstens den Vendor Name als Top-Level hat.

Klassennamen MÜSSEN in StudlyCaps deklariert werden.

:!: Anpassung für WB: Interfaces und abstrakte Klassen müssen im Namen zusätzlich mit dem Suffix Interface oder Abstract ergänzt werden.

Code der für PHP-5.3 und höher geschrieben wird MUSS formale Namespace nutzen.

Beispiel: <PHP> <?php php 5.3 und später: namespace Vendor\Model; class Foo { } </PHP> <PHP> <?php php 5.3 und später: namespace Vendor\Model;

class FooInterface { } </PHP> Code der für PHP-Versionen vor 5.3 geschrieben wird SOLL die Konvention der Pseudo-Namespaces mit Vendor_ Präfixes für Klassennamen benutzen. <PHP> <?php PHP 5.2.x und früher: class Vendor_Model_Foo { } </PHP> <PHP> <?php PHP 5.2.x und früher: class Vendor_Model_FooAbstract { } </PHP>

Klassen- Konstanten, Eigenschaften und Methoden

Das Term 'Klasse' bezieht sich auf alle Klassen, Interfaces und Traits.

Konstanten

Klassenkonstanten MÜSSEN vollständig in Großbuchstaben deklariert werden. Der Unterstrich trennt einzelne Worte. <PHP> <?php namespace Vendor\Model;

class Foo {

  const VERSION = '1.0';
  const DATE_APPROVED = '2014-08-12';

} </PHP>

Eigenschaften

Dieser Leitfaden vermeidet absichtlich jede Empfehlung in Bezug auf die Verwendung von $StudlyCaps, $camelCase oder $under_score Eigenschaftennamen.
Welche Namenskonvention auch benutzt wird, sie SOLLTE konsistent in einem vertretbaren Rahmen angewendet werden.

:!: Anpassung an WB: Es MUSS durchgehend überall das StudlyCaps Format benutzt werden, wobei jedem Bezeichner ein Kleinbuchstabe vorangestellt wird, der den Datentyp der Eigenschaft beschreibt. Derzeit definiert sind folgende Zuweisungen:

's' ⇒ String, 'i' ⇒ Integer/Ganzzahl, 'f' ⇒ Fließkommazahl, 'b' ⇒ boolean, 'a' ⇒ Array, 'o' ⇒ Objekt

<PHP> $sItemName = 'Something'; $iUserId = 2; $fPrice = 45.25; </PHP>

Methoden

Methodennamen MÜSSEN in camelCase() deklariert werden.

Manuela v.d.Decken 12.08.2014 20:35

en/dev/all/psr/psr-1.1432577829.txt.gz · Last modified: 03.06.2015 15:55 (external edit)