¿Debo usar @return self, esto o la clase actual? [cerrado]


Tengo un método que devuelve el objeto actual, ¿cómo puedo documentar esto?

/**
 * set something
 *
 * @return this
 */
public function setSomething(){
            // ...
    return $this;
}

O debería hacer @return self o @return Current_Class_Name?

Author: lucaswxp, 2011-08-04

4 answers

@return Current_Class_Name definitivamente funcionará y es lo que prefiero.

@return self puede funcionar bien con algunos programas también.

@return this es malo porque no es un nombre de tipo.

 27
Author: RiaD,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-28 11:48:32

Hay una Recomendación de Estándares PHP (PSR) actualmente en borrador (PSR-5) que propone @return this this se usa para indicar que se devuelve la misma instancia.

This esto, el elemento al que se aplica este tipo es exactamente la misma instancia que la clase actual en el contexto dado. Como tal, este tipo es una versión más estricta de static ya que, además, la instancia devuelta no solo debe ser de la misma clase, sino también de la misma instancia.

Este tipo se utiliza a menudo como valor de retorno para los métodos que implementan el patrón de diseño de interfaz Fluent.

Esta notación es utilizada actualmente por IDE populares como PhpStorm y Netbeans.

 30
Author: g .,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-30 14:05:21
/**
 * set something
 *
 * @return self
 */
public function setSomething(){
            // ...
    return $this;
}

Puede usar el tipo "self" como @param o @return..

PHPDoc recomienda 'self' para referirse a self en object..

Fuente: http://www.phpdoc.org/docs/latest/references/phpdoc/types.html

 7
Author: MURATSPLAT,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-22 20:08:41

Esta pregunta es bastante antigua, ¡pero solo quiero compartirla con todos!

AL MENOS para el que usa NetBeans 8.1.. esta notación hace que el autocompletado de código funcione bien en IDE:

/**
 * Method that returns $this instance (using late state binding)
 * @return static
 */
 public function iWillReturnMyself ( ) {
     return $this;
 }

Digo AL MENOS para los usuarios de NetBeans8.1, pero puede funcionar en versiones anteriores y/u otros IDE también=]

 4
Author: Di3g0,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-11 07:00:07