title: "Inline" Class Instantiation in PHP? (For Ease of Method Chaining)
body: <p>An idiom commonly used in OO languages like Python and Ruby is instantiating an object and chaining methods that return a reference to the object itself, such as:</p>
  
  <pre><code>s = User.new.login.get_db_data.get_session_data
  </code></pre>
  
  <p>In PHP, it is possible to replicate this behavior like so:</p>
  
  <pre><code>$u = new User();
  $s = $u-&gt;login()-&gt;get_db_data()-&gt;get_session_data();
  </code></pre>
  
  <p>Attempting the following results in <code>syntax error, unexpected T_OBJECT_OPERATOR</code>:</p>
  
  <pre><code>$s = new User()-&gt;login()-&gt;get_db_data()-&gt;get_session_data();
  </code></pre>
  
  <p>It seems like this could be accomplished using static methods, which is probably what I'll end up doing, but I wanted to check the lazyweb: <strong>Is there actually a clean, simple way to instantiate PHP classes "inline" (as shown in the above snippet)</strong> for this purpose?</p>
  
  <p>If I do decide to use static methods, <strong>is it too sorcerous to have a class's static method return an instantiation of the class itself</strong>?  (Effectively writing my own constructor-that-isn't-a-constructor?)  It feels kind of dirty, but if there aren't too many scary side effects, I might just do it.</p>
  
  <p>I guess I could also pre-instantiate a UserFactory with a get_user() method, but I'm curious about solutions to what I asked above.</p>