PHP指定方法的返回类型提示

时间:2023-01-17 19:21:06

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 ConstructPDOObject($dbname) 
{
      $hostname =self::HOSTNAME;
        $username = self::USERNAME;
        $password = self::PASSWORD;
        $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
        return $dbh;
}

And I want, whenever I call the above method, the IDE will show me the methods for PDO.

我想,每当我调用上面的方法时,IDE都会向我展示PDO的方法。

How to add the type hint?

如何添加类型提示?

5 个解决方案

#1


23  

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

在Aptana,PDT,Zend Studio和其他IDE中,您可以为php方法添加类型提示,如下所示:

/**
 * 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.

类名放在文档块的@return属性之后,表示方法的返回类型。例如。对于示例方法,PDO是返回的类名。附加说明“与数据库的连接”用于向其他开发人员提供返回值的有意义的描述,这不是必需的,但建议使用。

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.

以这种方式记录您的php方法的一个好处是,您可以使用phpDocumentor或doxygen生成文档。

#2


19  

For future reference, this is implemented for PHP 7, with the following syntax (quoted from source):

为了将来参考,这是针对PHP 7实现的,具有以下语法(引自源代码):

function foo(): array {
    return [];
}

To answer your question now, as of PHP 7 (released around end of 2015) you will be able to do the following (as an example):

要回答您的问题,从PHP 7(2015年底发布)开始,您将能够执行以下操作(作为示例):

<?php

function ConstructPDOObject($hostname, $dbname, $username, $password): PDO 
{
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    return $dbh;
}

The specification also allows for type hinting within and against interfaces; for those of us interested in adhering to SOLID principles.

该规范还允许在接口内和接口上进行类型提示;对于我们这些有兴趣遵守SOLID原则的人。

Source and more information: https://wiki.php.net/rfc/return_types

来源和更多信息:https://wiki.php.net/rfc/return_types

#3


5  

PHP doesn't support type hinting on return types. Perhaps you should add a documentation block declaring the return type and maybe your IDE will pick that up (I don't know if it will or not).

PHP不支持返回类型的类型提示。也许你应该添加一个声明返回类型的文档块,也许你的IDE会选择它(我不知道它是否会)。

#4


4  

The IDE hinting is done via comments. Here is an example from one of my ZEND Front Plugins.

IDE提示是通过注释完成的。以下是我的一个ZEND Front Plugins的示例。

<?php

/**
 * Initializes Application wide authentication
 *
 * @author Lance Rushing
 * @since  2009-06-01
 * @param  Zend_Session $session
 * @return Zend_Auth  <--- gives IDE Hint
 */
protected function initAuth($session)
{
    $auth = Zend_Auth::getInstance();
    require_once 'AuthStorage.php';
    $auth->setStorage(new My_AuthStorage($session));
    return $auth;
}

#5


3  

Return type hints are only supplied by your IDE. Zend studio and PDT support the PHPDocumentor style doc blocks.

返回类型提示仅由IDE提供。 Zend studio和PDT支持PHPDocumentor样式的doc块。

http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.return.pkg.html

#1


23  

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

在Aptana,PDT,Zend Studio和其他IDE中,您可以为php方法添加类型提示,如下所示:

/**
 * 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.

类名放在文档块的@return属性之后,表示方法的返回类型。例如。对于示例方法,PDO是返回的类名。附加说明“与数据库的连接”用于向其他开发人员提供返回值的有意义的描述,这不是必需的,但建议使用。

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.

以这种方式记录您的php方法的一个好处是,您可以使用phpDocumentor或doxygen生成文档。

#2


19  

For future reference, this is implemented for PHP 7, with the following syntax (quoted from source):

为了将来参考,这是针对PHP 7实现的,具有以下语法(引自源代码):

function foo(): array {
    return [];
}

To answer your question now, as of PHP 7 (released around end of 2015) you will be able to do the following (as an example):

要回答您的问题,从PHP 7(2015年底发布)开始,您将能够执行以下操作(作为示例):

<?php

function ConstructPDOObject($hostname, $dbname, $username, $password): PDO 
{
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    return $dbh;
}

The specification also allows for type hinting within and against interfaces; for those of us interested in adhering to SOLID principles.

该规范还允许在接口内和接口上进行类型提示;对于我们这些有兴趣遵守SOLID原则的人。

Source and more information: https://wiki.php.net/rfc/return_types

来源和更多信息:https://wiki.php.net/rfc/return_types

#3


5  

PHP doesn't support type hinting on return types. Perhaps you should add a documentation block declaring the return type and maybe your IDE will pick that up (I don't know if it will or not).

PHP不支持返回类型的类型提示。也许你应该添加一个声明返回类型的文档块,也许你的IDE会选择它(我不知道它是否会)。

#4


4  

The IDE hinting is done via comments. Here is an example from one of my ZEND Front Plugins.

IDE提示是通过注释完成的。以下是我的一个ZEND Front Plugins的示例。

<?php

/**
 * Initializes Application wide authentication
 *
 * @author Lance Rushing
 * @since  2009-06-01
 * @param  Zend_Session $session
 * @return Zend_Auth  <--- gives IDE Hint
 */
protected function initAuth($session)
{
    $auth = Zend_Auth::getInstance();
    require_once 'AuthStorage.php';
    $auth->setStorage(new My_AuthStorage($session));
    return $auth;
}

#5


3  

Return type hints are only supplied by your IDE. Zend studio and PDT support the PHPDocumentor style doc blocks.

返回类型提示仅由IDE提供。 Zend studio和PDT支持PHPDocumentor样式的doc块。

http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.return.pkg.html