1

I'm hoping I'm missing something simple here. I have a class that creates an array of another class. When I access the element(s) of the array, my IDE (honestly I have yet to try it in run-time because I'm not done coding) doesn't recognize the element as being from the originating class. So either I have to get used to it because that's how PHP works or I need to know the proper way to handle it. This may turn out to be a PHP IDE (PHPStorm) issue, however something doesn't feel right.

e.g. Let's say I have a class defined:

class memberClubAssoc  {

    private $_id;

    function __construct() {
        // do stuff
    }

    // other various functions
}

... and then I have another class. The function in question is dealingWithArray() in which I'm looping over the array of classes and accessing the class instances and then trying to use them as class instances.

class member {
    private $_id;

    private $_aClubAssociations;

    function __construct() {
        // do stuff

        $this->_aClubAssociations = array();
    }

    // fill in the array with instances of the memberClubAssoc class
    function someFunction() {
        $this->_aClubAssociations[0] = new memberClubAssoc(1);
        $this->_aClubAssociations[1] = new memberClubAssoc(2);
        // ...
    }

    // here's the function in question
    function dealingWithArray() {
        foreach ($this->_aClubAssociations as $clubAssoc) {
            // how does PHP know that $clubAssoc is actually an instance of 
            // memberClubAssoc? - PHPStorm certainly does not because when
            // I try to access a method on the class PHPStorm says it 
            // doesn't exist.
        }
    }
}

1 Answer 1

1

use references like

function dealingWithArray() {
    foreach ($this->_aClubAssociations as $clubAssoc) {
        /** @var $clubAssoc memberClubAssoc **/
    }
}

now php storm know that the data in $clubAssoc is a class from type memberClubAssoc. you don't make something wrong if you handle array values you need to tell PHPStorm what type of data is returned because PHPStorm can't get this info by itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.