lovespring
lovespring

Reputation: 19569

How to explicitly declare the type of a member var in a php class

If I can explicitly declare the type of a member var (esp. other class as a member), them my IDE( such as Dreamweaver) can know the member's member.

class PHPClass(){
    OtherClass $member_var;
}

Upvotes: 10

Views: 5023

Answers (3)

Alexander Hugestrand
Alexander Hugestrand

Reputation: 81

In PHP 7.4, property definitions can have type declarations.

https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.typed-properties

Upvotes: 1

Petah
Petah

Reputation: 46050

The only way is to use documentation like so:

class MyClass {

    /**
     * @var OtherClass This is my other class
     */
    private $other;

}

Upvotes: 17

John Conde
John Conde

Reputation: 219804

You can't do that in PHP but you can come close by using type hinting:

class PHPClass
{
    protected $member_var;

    public function __construct(OtherClass $member)
    {
        $this->member_var = $member;
    }
}

I don't know if that will help you in Dreamweaver, though.

Upvotes: 6

Related Questions