Hi Mark,
I know, it's possible to write an even more sophisticated thing in such dynamic languages like PHP

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:
<?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:
/** @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:
// 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->
|