NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
How to use type hints for "fluent interfaces"?


Joined: 21 Mar 2008
Posts: 8
Reply with quote
Hi,

i'm trying to write some code using "fluent interface" model. It means that every class method returns $this after comletion, so the programmer can write code like this:

Code:
$value = Gui::GetControl('name')->setValue('test1')->getValue();


Code autocompletion works fine if I specify type of returning variable, for example:

Code:
/**
 * Method 1
 *
 * @return GuiControl GUI object
 */


Unfortunately there is a problem with class inheritance: for phpED "return $this;" in method of derived class points to the type of base class rather than derived one. Is there any way to tell phpED that I want him to guess the current class type? I do not wish to write unique phpDoc comments regarding type of returned object for every derived class. I think this functionality would be fine:

Code:
/**
 * Method 1
 *
 * @return this GUI object
 */
View user's profileFind all posts by aargothSend private message


Joined: 14 Jun 2009
Posts: 13
Reply with quote
IMHO, any method that returns $this should automatically be picked up as the object the method was called from. It can be nothing else...
View user's profileFind all posts by FloydianSend private message


Joined: 21 Mar 2008
Posts: 8
Reply with quote
Quote:
IMHO, any method that returns $this should automatically be picked up as the object the method was called from. It can be nothing else...[/qote]Yes, thats true. I think this shouldn't be a problem for phpEd to do it. The problem is that writting "this" as returned data type breaks compatibility with phpDoc specification, so maybe this is the main reason why phpEd does not support fluent interfaces already.
View user's profileFind all posts by aargothSend private message


Joined: 24 Nov 2010
Posts: 21
Reply with quote


Situation don't change still?
View user's profileFind all posts by exedSend private message
Guru master

Joined: 24 Jul 2009
Posts: 717
Reply with quote
As far as I'm aware PHPDoc is the only way to get it to work in PhpED.

Code:
<?php

  class App
  {
    /**
    * @return App
    */
    function GetThis($A = 0)
    {
      echo $A;
      return $this;
    }
    /**
    * @return App
    */
    function OK()
    {
      echo 'OK';
      return $this;
    }
  }
  $Ap = new App();
  $Ap->GetThis(2)->OK();
  $Ap->GetThis()->

?>


I do sometimes use a competitor product to PhpED and that does provide code completion based on the actual return value, so it would be nice if PhpED did it as well.

It would be very useful for working with API's such as with Zend framework and Magento, where PHPDoc is not always fully utilised.

Even if full type tracking wasn't implemented, checking for a simple return $this would probably be straight-forward.
View user's profileFind all posts by plugnplaySend private message
Site Admin

Joined: 13 Jul 2003
Posts: 7861
Reply with quote
in 6233 your sample works fine without any phpdocs

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Guru master

Joined: 24 Jul 2009
Posts: 717
Reply with quote
Brilliant Very Happy That's going to make Magento a lot easier
View user's profileFind all posts by plugnplaySend private message
How to use type hints for "fluent interfaces"?
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic