<?php class MaClasse { protected $a; function __construct($valeur) { $this->a = $valeur; } function afficher() { echo $this->a; } } $monInstance = new MaClasse("hello les sio"); $monInstance->afficher(); // affiche hello les sio
class MaClasse { public static $prefixe = "hello"; protected $mot; public function __construct($mot) { $this->mot = $mot; } public function afficher() { echo self::$prefixe . ' ' . $this->mot; } public static function definirPrefixe($prefixe) { self::$prefixe = $prefixe; } } $obj1 = new MaClasse("world"); $obj1->afficher(); echo "<br/>"; MaClasse::definirPrefixe("strange"); $obj2 = new MaClasse("world"); $obj2->afficher(); echo "<br/>"; $obj1->afficher(); echo "<br/>"; Maclasse::$prefixe = "étrange"; $obj2->afficher(); echo "<br/>"; $obj1->afficher(); echo "<br/>";