stdclass with autocompletion

Hello!
I have a record book. This record book was written as an array and as an object.

 I want to use this data in other methods.
PhpStorm has a suggestion function for this record?

--------------------------------------------------------
class myClass{

        private $_fooRecord = null;
        private $_baaRecord = null;

        public function foo(){
            $book = new stdClass;
            $book->title = "Harry Potter and the Prisoner of Azkaban";
            $book->author = "J. K. Rowling";
            $book->publisher = "Arthur A. Levine Books";
            $book->amazon_link = "http://www.amazon.com/dp/0439136369/";

            /** @var $this->_fooBook  */
            $this->_fooRecord = $book;

            return;
        }

        public function baa(){
            $book = array(
                "title" => "Harry Potter and the Prisoner of Azkaban",
                "author" => "J. K. Rowling",
                "publisher" => "Arthur A. Levine Books",
                "amazon_link" => "http://www.amazon.com/dp/0439136369/"
            );

            /** @var _baaRecord  */
            $this->_baaRecord = book;

            return;
        }

        public function test(){

            // how can i autocomplet array ???
            $author1 = $this->_fooRecord-> ???;

             // how can i autocomplet object ???
            $author2 = $this->_baaRecord[' ???? '];
        }
    }

    $testClass = new myClass();
    $testClass->foo();
    $testClass->baa();
    $testClass->test();


-------------------------------------------------------------------

Sincerely yours

Stephan

2 comments
Comment actions Permalink

Based on your code example:

1) Generally speaking -- why do use intermediate variable and not working with class variable directly? This way you just making IDE's life harder (you are describing structure of some local variable.. but then (in another method) working with completely different class variable (from IDE point of view)).

2) Why use stdClass -- why not create special class (e.g. BookDetails) and work with it instead?

3) Regarding arrays: recently quite a few tickets were fixed in this regard (completion of array keys) -- wait for next EAP or stable build and see if it will improve your situation

4) Regarding stdClass: http://youtrack.jetbrains.com/issue/WI-12220 . Once again -- why via intermediate variable? IDE cannot analyze everything, it does not have all the info that PHP itself has when executing this code.


In any case, here is an idea (stdClass approach):

1) Create some simple class and describe all fields (e.g. BookDetails). It will never be executed -- it is just for IDE to see:

class BookDetails
{     public $title = '';     public $author = '';     public $publisher = '';     public $amazon_link = ''; }


2) In your code, work as you are working right now, just trick IDE telling that $_fooRecord is instance of that class using PHPDoc comment

class myClass
{     /** @var BookDetails */
    private $_fooRecord = null;
    ...


For PHP $_fooRecord will be instance of stdClass .. but from IDE point of view it will be instance of BookDetails and therefore it should offer code completion for those fields.

0
Comment actions Permalink

Hello!

Thanks for the information.
I get an array from a database.
This array contains the information on a cart.
This array is very complicated.

For example:
----------------------------------

$cart = array(
            'user' => array(
                'firstname' => 'Max',
                'lastname' => 'mustermann',
                'street' => 'Lange Straße',
                'zip' => '123456'
            ),
            'programm1' => array(
                'description' => 'bla',
                'price' => '100.20',
                'implementation' => array(
                    'street' => 'Dorfweg',
                    'housenumber' => '25',
                    'times' => array(
                        'first' => '12:00',
                        'second' => '14:00'
                    )
                )
            )
        );




------------------------------------------------

I want to use this array in my application.
Therefore I am looking for a way to autocomplete this array.

Sorry for my bad English.

Sincerely yours

Stephan
0

Please sign in to leave a comment.