Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293443

Use @return $this to mark that a method returns itself

$
0
0

Let's assume you have something like this:

<?php
class Foo {
  public function
external() {
   
$this->setValue1()->setValue2();
  }

 
/**
   * @return Foo
   * Returns the called object.
   */
 
protected function setValue1() {
   
$this->foo = mt_rand(1, 10);
    return
$this;
  }

 
/**
   * @return Foo
   * Returns the called object.
   */
 
protected function setValue2() {
   
$this->foo = mt_rand(1, 10);
    return
$this;
  }
}
?>

What about document this using @return Foo|$this, so it's a bit easier to read, and can be understood by IDEs, so they don't show problems when calling protected methods in a chain.


Viewing all articles
Browse latest Browse all 293443

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>