0
<?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();   
?>

I don't understand why the second variable does not show anything? But in the array it does exist.

3
  • Are you trying to set a private variable? Commented Oct 10, 2012 at 16:41
  • You are setting unAttributPrive and echoing autreAtribut. Try echo $obj->unAttributPrive;. Commented Oct 10, 2012 at 16:44
  • Sorry guys !! i thought it was the same !! thanks for helping me ! Commented Oct 10, 2012 at 20:21

2 Answers 2

2

You're setting unAttributPrive, but getting autreAtribut.

Sign up to request clarification or add additional context in comments.

3 Comments

I set both : $obj->attribut = 'Simple test'; $obj->unAttributPrive = 'Autre simple test';
@satyres: Correct, but you're echo $obj->autreAtribut; rather than echo $obj-> unAttributPrive;
Yes it's true !! Sorry guys !! i thought it was the same !! thanks for helping me !
0

I'm gonna go out on a whim and guess because you are spelling your variable names wrong. If you are looking to echo $obj->unAttributPrive

3 Comments

This is not the main problem : i don't uderstand why the second variable does not show his content ?
Because there is no content to show? I think Josh and I are trying to say you haven't stored anything into that second variable.
Sorry guys !! i thought it was the same !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.