Variable of type Object within a class

Asked

Viewed 402 times

3

I don’t know if it’s possible, but I’d like to do as I do in java the following code in PHP

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;
    }

In the PHP I can’t put in a class in PHP

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

}

In PHP this is wrong. How to solve this?

1 answer

4


PHP is a language with dynamic typing, that is, unlike Java we do not define the type of the variable.

To guarantee the type of our property we use the hinting type together 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;
    }
}
  • I get it. Thank you.

Browser other questions tagged

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