Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

intialisation de variables privees php > 5

2 réponses
Avatar
thierry
bonjour

soir le code suivant:

<?php
class test {
private $p1;
function __construct($options) {
$this->p1 = 1;
foreach ($options as $option => $valeur) {
echo "this->$option = $valeur\r\n";
${'this->'.$option} = $valeur;
}
}

public function lire_p1() {
return $this->p1;
}
}

$test = new test(array('p1' => 2));
echo "-------------------------\r\n";
echo $test->lire_p1();
?>

il affiche systématiquement 1, alors que je pense qu'il devrait afficher 2 ?

ai-je mal compris qqch ?

merci d'avance

thierry

PS: test fait sous php 5.1.6

2 réponses

Avatar
Mickaël Wolff
${'this->'.$option} = $valeur;


$this->$option = $valeur ;

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

Avatar
thierry
effectivement, mille merci