Viewed   135 times

Here is my code:

function phpwtf(string $s) {
    echo "$sn";
}
phpwtf("Type hinting is da bomb");

Which results in this error:

Catchable fatal error: Argument 1 passed to phpwtf() must be an instance of string, string given

It's more than a little Orwellian to see PHP recognize and reject the desired type in the same breath. There are five lights, damn it.

What is the equivalent of type hinting for strings in PHP? Bonus consideration to the answer that explains exactly what is going on here.

 Answers

4

Prior to PHP 7 type hinting can only be used to force the types of objects and arrays. Scalar types are not type-hintable. In this case an object of the class string is expected, but you're giving it a (scalar) string. The error message may be funny, but it's not supposed to work to begin with. Given the dynamic typing system, this actually makes some sort of perverted sense.

You can only manually "type hint" scalar types:

function foo($string) {
    if (!is_string($string)) {
        trigger_error('No, you fool!');
        return;
    }
    ...
}
Thursday, October 6, 2022
5

PHP 7.4 will support typed properties like so:

class Person
{
    public string $name;
    public DateTimeImmutable $dateOfBirth;
}

PHP 7.3 and earlier do not support this, but there are some alternatives.

You can make a private property which is accessible only through getters and setters which have type declarations:

class Person
{
    private $name;
    public function getName(): string {
        return $this->name;
    }
    public function setName(string $newName) {
        $this->name = $newName;
    }
}

You can also make a public property and use a docblock to provide type information to people reading the code and using an IDE, but this provides no runtime type-checking:

class Person
{
    /**
      * @var string
      */
    public $name;
}

And indeed, you can combine getters and setters and a docblock.

If you're more adventurous, you could make a fake property with the __get, __set, __isset and __unset magic methods, and check the types yourself. I'm not sure if I'd recommend it, though.

Sunday, October 2, 2022
4

@CharlotteDunois pointed this out, but Twig 2.0 requires >=PHP7.0, so in your environment (php 5.6) you can't use Twig 2.0. From the Twig official documentation:

Prerequisites

Twig needs at least PHP 7.0.0 to run.

Notice that for PHP5.x branch you still have Twig 1.x aviable

Friday, November 25, 2022
1

/* @var ClassName $object */ is a non-valid PHPDOC comment and is not parsed in the current version of Web IDE. Use double asterisks to make it work:

/** @var ClassName $object */

Also, you can annotate $array in foreach($array as $var) with /** @var ClassName[] $array */ and $var type will be deduced automatically.

Wednesday, September 28, 2022
 
2

The exception handler in Laravel 5.5 checks if the exception has a report method, and if so, let the exception handle the reporting itself. This means that the handler will see your report method, and call $e->report();, but your report method requires a parameter.

This is done in Handler::report.

You either need to remove the parameter in your report method (it should be reporting itself; $this) if you want to use this functionality, or rename the method if you don't want Laravel to call it (and fail).

Relevant: Laravel 5.5 Adds Support for Custom Exception Reporting

Friday, December 9, 2022
 
kartik
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :