I
I
igor11122019-02-21 16:57:33
PHP
igor1112, 2019-02-21 16:57:33

Why is PHPDoc needed in php-7?

Please tell me why you need to specify the type of the method parameter and the type of what the method returns in phpDoc, if in the seventh version of php in the method itself you can specify the type of parameters and the type of the return value? It's a pointless duplication. Why do they do it?

/**
     * Very important function
     *
     * @param string $param
     * @return array
     */
    function func(string $param) :array {
          
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan Rodionov, 2019-02-21
@igor1112

There are scenarios in which a function returns values ​​that cannot be described by a hint. Roughly speaking like this

/**
*  @return Product|false
*/
public function getProduct(int $id)
{
    // ...
}

As a rule, this is wrong (Bitrix especially likes to do this), but the dock block accurately describes what the method can return. Here, by the way, is another subtle point: you can write not bool, but false, because even though the type is bool, the true value will never be there. In short, these things are needed)

S
Stanislav Tamat, 2019-03-01
@YokiToki

You can also add that they are used by static analyzers , as well as various IDEs (for the same static analysis)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question