Variable of type Object within a class

3

I do not know if it is possible, but I would like to do as%% with the following code in java

in JAVA

public class Not_req {

    private Cabacelho_Req cab;
    private Requisicao req;

    public Cabacelho_Req getCab() {
        return cab;
    }

    public void setCab(Cabacelho_Req cab) {
        this.cab = cab;
    }

No PHP can not put in a class in PHP

class Not_req {
    private Cabacelho_Req $cab;
    private Requisicao $req;

}

In PHP this goes wrong. How do I resolve this?

    
asked by anonymous 05.06.2015 / 20:40

1 answer

4

PHP is a language with dynamic typing , that is, other than Java, we have not defined the type of the variable .

To guarantee the type of our property we use type hinting along with the encapsulation.

To get what you want to do so:

public class Not_req {

    private $cab;
    private $req;

    public function getCab() {
        return $this->cab;
    }

    public function setCab(Cabacelho_Req $cab) {
        $this->cab = $cab;
    }
}
    
05.06.2015 / 20:44