PHP Specify the Return Type Hints for a Method

后端 未结 5 1079
庸人自扰
庸人自扰 2021-01-07 23:18

What is the correct syntax for me to specify the return type hints for a method?

For example, I have such a method:

private static function Construct         


        
5条回答
  •  礼貌的吻别
    2021-01-07 23:49

    Within Aptana, PDT, Zend Studio and other IDE's you can add type hinting to php methods as follows:

    /**
     * Constructs a new PDO Object and returns it
     *
     * @param string $dbname name of the database to connect to
     * @return PDO connection to the database
     */
    private static function ConstructPDOObject($dbname) 
    {
          $hostname =self::HOSTNAME;
          $username = self::USERNAME;
          $password = self::PASSWORD;
          $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
          return $dbh;
    }
    

    The class name is placed after the @return attribute of the documentation block to signify the return type of the method. E.g. In the case of your example method, PDO is the class name that is returned. The additional description "connection to the database" is used to provide a meaningful description of the returned value to other developers, this is not required but is advised.

    One of the great things about documenting your php methods in this manner, is that you can then generate documentation using either phpDocumentor or doxygen.

提交回复
热议问题