NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Code completion: Iterator and ArrayAccess usage


Joined: 06 Apr 2007
Posts: 48
Location: Gdansk, POLAND
Reply with quote
Since PHP 5.0 we have a great feature - implementable Iterators:
http://www.php.net/~helly/php/ext/spl/interfaceIterator.html

Unfortunately, phpEd does not use its declaration for type analysis in code completion:
Code:
<?php
class MyIterator implements Iterator
{
    /**
    * @return myObject
    */
    function current() {
        (...)
        return $someObject;
    }
    (...)
}

$myIterator = new MyIterator(...);
foreach ($myIterator as $key => $element)
{
    // Failed to identify type of "$element"
    $element->|
}
?>


When Iterator is implemented, it should use the return value of current() method to find type of $element in foreach.

It would be also great, if phpEd uses ArrayAccess interface:
http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html

Code:
<?php
class MyArray implements ArrayAccess
{
    /**
    * @return myObject
    */
    function & offsetGet($offset)
        (...)
        return $someObject;
    }
    (...)
}

$myArray = new MyIterator(...);
$myArray[0]->|
// phpEd doesn't even try to recognize the type :(
?>


It would really speed up working with object collections with PHP5.
View user's profileFind all posts by NatanielSend private messageVisit poster's website


Joined: 27 Mar 2006
Posts: 77
Reply with quote
So has anyone found a trick or anything to get this to work. It would make my life much easier and obviously Nataniel's.

TW
View user's profileFind all posts by twSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
I think it's answered there http://forum.nusphere.com/code-completion-for-php-spl-dom-pdo-and-dom-t1733.html

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


Joined: 06 Apr 2007
Posts: 48
Location: Gdansk, POLAND
Reply with quote


No, it's not? Adding SPL.php to the include list only allows to complete methods/classes of SPL itself, not the methods of custom objects using SPL features.
View user's profileFind all posts by NatanielSend private messageVisit poster's website


Joined: 27 Mar 2006
Posts: 77
Reply with quote
It doesn't work for me either.
View user's profileFind all posts by twSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Code completion recognizes many things and you can help it serve you better by providing prototypes and type hints. Could you please post a more detailed info regarding what does not work for you?

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


Joined: 27 Mar 2006
Posts: 77
Reply with quote


The $D-> is where I pressed ctrl N it should show the data var as an available option.

I really don't know how feasible this is to do but I would be a giant help.


TW
View user's profileFind all posts by twSend private message


Joined: 27 Mar 2006
Posts: 77
Reply with quote
I saw this somewhere and I thought I would give it a try and it works.

Quote:
/** @var a */


Code:

class a  {
    public $data;
    public function __construct( $d ){
        $this->data = $d;
    }
}

class b implements Iterator {
    private $key;
    private $data;

    public function __constructor( $d ){
        $this->data = $d;
    }
    public function current () {
        return new b( $this->data );

    }
    public function key (){
        return $this->key;
    }
    public function next (){;}
    public function rewind (){;}
    public function valid (){;}
}

$a = new b( 'me' );
foreach( $a as $d ){
    /** @var a */
    $d->data;
}


I can work with this I think. I haven't tried anything but the iterator but I assume it will work similarly.
View user's profileFind all posts by twSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
did you mean this?
Code:
    public function current () {
        return new a( $this->data );

    }


otherwise I see no way how to get a from b and how would code insight recognize this... Even with correction, I doubt if it's clear...

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


Joined: 27 Mar 2006
Posts: 77
Reply with quote
alas, I forgot some of the most important aspects of the object didn't I. Embarassed

What I was trying to point out is I can add a /** @var a */ to make the $d look like the correct data type. It is crude but it works.

Code Completion doesn't work complete from the current() method unless you directly call the current() method.
View user's profileFind all posts by twSend private message
Code completion: Iterator and ArrayAccess usage
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