How to access parent class attributes in PHP?

Asked

Viewed 942 times

1

How do I access the parent class attributes once I instated the child?

Direct access $Filho->atributoPai doesn’t work.

Access by function $Filho->getterPai doesn’t work.

For every inherited child I must make getters to your father’s attributes?

<?php

    class Pessoa {
        function __construct($nome, $sexo, $idade) {
            $this->nome = $nome;
            $this->sexo = $sexo;
            $this->idade = $idade;
        }
        function getNome() {
            return $this->nome;
        }
        private $nome;
        private $sexo;
        private $idade;
    }
    class Amigo extends Pessoa {
        function __construct($nome, $sexo, $idade, $diaDoAniversario) {
            parent::__construct($nome, $sexo, $idade);
            $this->diaDoAniversario = $diaDoAniversario;
        }
        private $diaDoAniversario;
    }

    $joao = new Amigo("Jonis", "masc", 20, 30);
    echo "Nome: $joao->nome";  // Notice: Undefined property: Amigo::$nome
    echo "Nome: $joao->getNome()"; // Undefined property: Amigo::$getNome

?>
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

One of the problems is that you can’t keep forcing a complex expression into a string, has to concatenate, or with keys.

You also want to access the field (I don’t call it an attribute because there’s no documentation that says it in any language mainstream who cares about setting things right) then you need to make it public, otherwise you won’t even be able to access it in the upper class object.

To tell you the truth I don’t know if I should do this in PHP. Languages script are not suitable for complex systems. It’s like using OOP in microservices, if actually the services are micro, and one of the reasons for doing this is to make the code not complex, then OOP is unnecessary, which is a style for organizing complexity. But in general people are adopting things without thinking about what they’re doing and why they adopted it.

In fact, the upper class should not be instantiated, so it should be abstract.

class Pessoa {
    function __construct($nome, $sexo, $idade) {
        $this->nome = $nome;
        $this->sexo = $sexo;
        $this->idade = $idade;
    }
    function getNome() {
        return $this->nome;
    }
    public $nome;
    private $sexo;
    private $idade;
}
class Amigo extends Pessoa {
    function __construct($nome, $sexo, $idade, $diaDoAniversario) {
        parent::__construct($nome, $sexo, $idade);
        $this->diaDoAniversario = $diaDoAniversario;
    }
    private $diaDoAniversario;
}

$joao = new Amigo("Jonis", "masc", 20, 30);
echo "Nome: {$joao->nome}";
echo "Nome: " . $joao->getNome();

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thank you, don’t really force a complex structure inside a solved string! It was an exercise proposed by the advisor to be developed in php itself.. No more that, worth !!

Browser other questions tagged

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