Saurabh datta
Saurabh datta

Reputation: 62

PHP 7 Type Hinting Issues

<?php
namespace XYZ\Model;

interface user{
public function getName() : string;
}

?>

Now, what happens is that string is assumed to be the type XYZ\Model\string and hence, any classes I make implementing the interface don't match up (in different namespaces).

If however I do a \string, the code fails with Scalar type declaration must be unqualified.

Besides, how many types of boolean can there be? After removing some hints, I got: Return value of xxxxx::save() must be an instance of boolean, boolean returned in xxxxxx.php:41

Upvotes: 4

Views: 1581

Answers (2)

Saurabh datta
Saurabh datta

Reputation: 62

UPDATE: Everything's alright with PHP. it's boolean that doesn't support scalar type hinting.

Upvotes: -1

Mikey
Mikey

Reputation: 2704

Tested out the following Code on 7.0.3 and it works fine, bearing in mind I added a missing ; next to your namespace definition.

namespace XYZ\Model;

interface user {
    public function getName(): string;
}


class Test implements User {
    public function getName(): string {
        return 'SomeName';
    }
}

$t = new Test;
echo $t->getName();

Outputs:

SomeName

Upvotes: 2

Related Questions