<?php
class MaClasse
{
private $attributs = array();
private $unAttributPrive;
public function __get ($nom)
{
if (isset ($this->attributs[$nom]))
return $this->attributs[$nom];
}
public function __set ($nom, $valeur)
{
$this->attributs[$nom] = $valeur;
}
public function afficherAttributs()
{
echo '<pre>', print_r ($this->attributs, true), '</pre>';
}
}
$obj = new MaClasse;
$obj->attribut = 'Simple test';
$obj->unAttributPrive = 'Autre simple test';
echo $obj->attribut;
echo $obj->autreAtribut;
$obj->afficherAttributs();
?>
我不明白,爲什麼第二個變量沒有顯示任何東西? 但在數組中它確實存在。超載及陣列
你們是不是要設置一個私有變量? –
您正在設置'unAttributPrive'並回顯'autreAtribut'。試試'echo $ obj-> unAttributPrive;'。 – air4x
對不起傢伙!我以爲它是一樣的!感謝幫助我! – satyres