PHP: class (le getter et le setter)
Bonjour,
je suis un débitant en programmation orienté objet avec PHP je fais un exercice qui me complique un peu si quelqu'un a l'idée peut m'aider s'il vous plait. Voilà le codes ci-dessous :
page : classe3.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php
class client{
private $prenom;
private $nom;
private $age;
public function getPrenom() {
return $this−>prenom;
}
public function getNom() {
return $this−>nom;
}
public function getAge() {
return $this−>age;
}
public function setPrenom($prenom){
if (!preg_match("/^\s*(.+?)\s*$/", $prenom, $champs)) {
throw new Exception("Le prénom doit être non vide");
} else {
$this->prenom = $champs[1];
}
}
public function setNom($nom){
if (!preg_match("/^\s*(.+?)\s*$/", $nom, $champs)) {
throw new Exception("Le nom doit être non vide");
} else {
$this->nom = $champs[1];
}
}
public function setAge($age){
if (!preg_match("/^\s*(\d+)\s*$/", $age, $champs)) {
throw new Exception("L'âge doit être un entier positif ou nul");
} else {
$this->age = $champs[1];
}
}
function __construct($prenom, $nom, $age){
$this−>setPrenom($prenom);
$this−>setNom($nom);
$this−>setAge($age);
}
public function toHTML() {
return $this−>prenom . " \n " . $this−>nom . " \n " . $this−>age ;
}
}
?> |
page : index.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?PHP
include("classe3.php");
$merci=new client("CHICCO", "BETUKUTALA", 30);
echo $merci->toHTML() ;
try {
$merci=new client("xxxxx", "yyyyyyy", "gg");
} catch (Exception $e) {
print $e->getMessage();
}
//fin
exit();
?> |
NB: mon problème est que lors de l'exécution y a une erreur au niveau de mon constructeur.
Citation:
Fatal error: Call to undefined function setPrenom() in D:\Chicco\exercices\classe3.php on line 52
la ligne 52 c'est la ligne par laquelle j'ai fait la construction donc il arrete ici :
Code:
1 2 3 4 5
| function __construct($prenom, $nom, $age){
$this−>setPrenom($prenom);
$this−>setNom($nom);
$this−>setAge($age);
} |