Access private attributes of a class within another class

Asked

Viewed 1,025 times

1

I started studying PHPOO and I’m having difficulty accessing the attributes of the class Pessoa that are private within the class Login, follows the code

class person

class Pessoa {
    private $nome;
    private $idade;

    public function getNome(){
        return $this->nome;
    }

    public function setNome($n){
        $this->nome = $n;
    }

    public function getIdade(){
        return $this->idade;
    }

    public function setIdade($i){
        $this->idade = $i;
    }

    public function dados_nome() {
        $this->nome = "Fulano de Tal";
        $this->setNome($this->nome);
        //echo ''.$this->nome.' '.$this->sobrenome.' de '.$this->idade.' está online.';
    }

    public function dados_idade() {
        $this->idade = 21;
        $this->setIdade($this->idade);
    }
}

class Login

class Login {
    public $email;
    public $pass;

    public function logar() {
        $this->email = "[email protected]";
        $this->pass = "123456";

        if ($this->email == "[email protected]" and $this->pass == "123456") :
            $dados = new Pessoa;
            $dados->dados_nome();
            $dados->dados_idade();
            echo "Bem vindo ".$dados->nome." de ".$dados->idade." anos.";
        else :
            echo "Dados incorretos.";
        endif;
    }
}

Fatal error: Uncaught Error: Cannot access private Property Pessoa::$nome in C: xampp htdocs testes class.php:47 Stack trace: #0 C: xampp htdocs testes index.php(12): Login->logar() #1 {main} thrown in C: xampp htdocs testes class.php on line 47

  • Private properties can only be accessed by the class itself. You cannot instantiate the class Pessoa and want to directly access the property $nome where it is private, if you want to achieve this object you have two options: Change the visuality to public or access the method getNome(). Documentation

  • @Valdeirpsr Ué, I thought the getters and setters was for this, to access a private attribute outside or inside another class

  • 2

    they are, but you’re trying to access a private object directly. echo "Bem vindo ".$dados->nome." and as the property is private, it is not possible. You must use echo "Bem vindo ".$dados->getNome()."

2 answers

7

The goal of being private is precisely not being able to access (help in encapsulation), so I’m glad you’re having a hard time. One of the object orientation objectives is to hide the implementation details and one of the ways to do this is to let private members, if you try to access what is private, or are wanting to do something that can’t or the member shouldn’t be private, then you have to decide how to tidy up.

I won’t even try to state what’s right because almost every OOP code I see, especially in PHP, is wrong, so the solution is to do it all over again or stop doing OOP, after all almost nobody does it right, and then there are the evils of using this technique and it doesn’t gain the benefits it could bring if it did right, which even most people can’t say what real benefits it had to do that way.

But in this case it just seems I should have used the method getter name and age instead of accessing the field. Including because in the comment you seem to think you are calling the methods and are not.

One of the mistakes that people make is to call attribute what is actually a field.

Another common mistake is to abuse getters and setters, OOP really abhors them. The impression is that he didn’t even want to use them anyway, but he did because he saw someone doing it somewhere. The class has slightly better methods, despite very bad names and also does nothing very useful and maybe even wrong.

But the most serious error is the incorrect structuring of classes and creating things that don’t make sense and don’t help the code get better.

2


Declaring the attribute as private cannot be accessed directly from another class. You will only be able to access this attribute indirectly through a method. In the above example, to be able to access the attributes name and age, the class Person should be instantiated as an example below:

Pessoa $p = new Pessoa();
$p.getNome(); // acessa-se indiretamente o atributo nome
$p.getIdade(); // acessa-se indiretamente o atributo idade

Browser other questions tagged

You are not signed in. Login or sign up in order to post.