NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Code Insite with methods having mixed return values


Joined: 18 Jul 2006
Posts: 11
Location: Madeira Beach, FL
Reply with quote
Hi, I am just starting out on phpED and so far I like what I see. I am having a bit of trouble with code insite however. I have several classes that have methods that add items to a list that is stored in the class as a private property. The method can take any type of item and add it to the list and then return the item it added. The following code might explain this a little better.

Assume the following classes are defined.

Code:

class item_list
  {
  private $_items;

  public function __construct()
    {
    $this->_list = array();
    }

  public function addItem($item)
    {
    $this->_items[] = $item;
    return $item;
    }
  }


class foo_item
  {
  private $_name;

  public function __construct($name)
    {
    $this->_name = $name;
    }

  public function getName()
    {
     return $this->_name;
    }
  }


class bar_item
  {
  private $_name;

  public function __construct($name)
    {
    $this->_name = $name;
    }

  public function getName()
    {
     return $this->_name;
    }
  }


Now, I want to use some of these classes. I might do something like this.

Code:

$MyList = new item_list();
$MyFoo = $MyList->addItem(new foo_item('foo1'));
$MyBar = $MyList->addItem(new bar_item('bar1'));


After the above code if I type...
Code:

$MyFoo->

or
Code:

$MyBar->


and hit Ctrl-Space I get an error that says...

Quote:
Code completion is not available due to the following errors(s):
Failed to identify typf of the resulting value returned by "item_list::addItem()" method


Any idea how I can get this to work? Posibly with some crafty phpDoc tags or something? Question

_________________
- Mark
View user's profileFind all posts by marzilloSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8340
Reply with quote
Hi Mark,
I know, it's possible to write an even more sophisticated thing in such dynamic languages like PHP Smile but to get the IDE a right IDEa about what types are where, you'd start using inheritance and types which means "more constraints" or use more phpdoc comments.
With first approach (inheritance) I'd recommend you to define an ancestor class that will hold all common properties and methods that xxx_item classes have.
In this case your code might look like this:

Code:
<?php
class an_item
  {
  protected $_name;

  public function __construct($name)
    {
    $this->_name = $name;
    }

  public function getName()
    {
     return $this->_name;
    }
  }
 
class item_list
  {
  private $_items;

  public function __construct()
    {
    $this->_list = array();
    }

  public function addItem(an_item $item)
    {
    $this->_items[] = $item;
    return $item;
    }
  }


class foo_item extends an_item
  {
  public function __construct($name)
    {
    an_item::__construct($name);
    // some foo-item specific code goes there
    }

  public function getName()
    {
     // some foo-item specific code may be there
     return an_item::getName();
    }
  }


class bar_item extends an_item { }

$MyList = new item_list();
$MyFoo = $MyList->addItem(new foo_item('foo1'));
$MyBar = $MyList->addItem(new bar_item('bar1'));

$MyFoo->
 
?>


the key change is
public function addItem($item)
to
public function addItem(an_item $item)


alternatively you might have:
Code:
  /** @return an_item */
  public function addItem($item)
    {
    $this->_items[] = $item;
    return $item;
    }
  }


but it's closer to the 2nd approach, where phpdoc comments are used:

Code:

// class definitions remain the same but phpdoc comments are added to the following places:
$MyList = new item_list();
/** @var foo_item */
$MyFoo = $MyList->addItem(new foo_item('foo1'));
/** @var bar_item */
$MyBar = $MyList->addItem(new bar_item('bar1'));

$MyFoo->
 
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 18 Jul 2006
Posts: 11
Location: Madeira Beach, FL
Reply with quote
ddmitre,

Thanks for the quick reply. I tried both methods and both have a few drawbacks. The first method works but only methods and properties in the parent class of 'an_item' are discovered. Any methods and properties in 'foo_item' and 'bar_item' are not discovered by phpED. This however is what I would expect based on the current behavior of phpED.

The second method also works however it requires one to add the special phpDoc tag to the code when the code self documents. The drawback is if you forget to add the phpDoc you don’t get any code insite for that object.

I guess a third method would be to not pass objects to functions and/or methods by using the new keyword but rather create the instance of the object and assign it to a variable and then pass it along. For example.

Code:

$MyFoo = new foo_item('foo1');
$MyList->addItem($MyFoo);

// instead of...

$MyFoo = $MyList->addItem(new foo_item('foo1'));


I wonder if it could be possible for phpED to recognize this style of programming though. On the surface it seems reasonable. If a method or function is passed a parameter using the new keyword and returns that same parameter back then the return type should be considered the same type. Essentially, increase the scope in which phpED interprets the following code snippit.

Code:
$MyList = new list()


Anyway, thanks for the reply. I’ll use the phpDoc method for now unless another solution comes along.

_________________
- Mark
View user's profileFind all posts by marzilloSend private message
Code Insite with methods having mixed return values
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