neoteknic / Phue

Philips Hue client for PHP - fork by Neoteknic, extra features - soon new Entertainment API support
Other
13 stars 5 forks source link

Typed property error - in create user binary #6

Closed syntaxseed closed 2 years ago

syntaxseed commented 2 years ago

Hello,

Running the binary: ./vendor/bin/phue-create-user 192.168.1.2

Gives an error:

./vendor/bin/phue-create-user 192.168.1.2
Testing connection to bridge at 192.168.1.2
PHP Fatal error:  Uncaught Error: Typed property Phue\Client::$transport must not be accessed before initialization in /vendor/neoteknic/phue/library/Phue/Client.php:195
Stack trace:
#0 /vendor/neoteknic/phue/library/Phue/Command/Ping.php(27): Phue\Client->getTransport()
#1 /vendor/neoteknic/phue/library/Phue/Client.php(223): Phue\Command\Ping->send()
#2 /vendor/neoteknic/phue/bin/phue-create-user(25): Phue\Client->sendCommand()
#3 {main}
  thrown in /vendor/neoteknic/phue/library/Phue/Client.php on line 195

I fixed the error by adding to the Client constructor so that it initializes a transport:

//Client.php
public function __construct(string $host, ?string $username = NULL) {
        $this->setHost($host);
        $this->setUsername($username);
        $this->setTransport(new Http($this));    //  <--- Added this line.
    }
syntaxseed commented 2 years ago

Update: My PR also fixes a few errors related to PHP 8, including some type errors.

neoteknic commented 2 years ago

better, I fixed it just today in my projet, just by init prop with =null and add a ? to the type, but it's better to init in constructor, so I can remove the null check in getter

neoteknic commented 2 years ago

fixed