Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Casting Variable as Object type in foreach Loop

Within the following code, $quiz_object->personalities contains an array of Personality objects.

// Loop through each personality that exists for the quiz foreach($quiz_object->personalities AS $existing_personality) {      // Show all of the existing personalities     echo $existing_personality->GetQuizMakerPersonalityHTML(); } 

How do I "cast" (I think that's the right word) my variable $existing_personality within the foreach loop as the object type?

I wish to do this so that when I type $existing_personality->, I get the list of public functions available for that object type.

UPDATE

At the moment, Zend Studio doesn't know I am looping through an array of Personality objects within the loop, it just thinks it's a standard variable. However, it is a type and my code works perfectly well. I just want the IDE hints on my variable within the foreach loop.

Just so that I'm clear, the hints appear for every other object, if I have:

$personality_object = new Personality();  // I get the IDE hints here echo $personality_object->MyFunction(); 

But as soon as I start looping in a foreach, Zend has no way of knowing that I'm looping through an array of Objects.

This is how the array of personalities is defined initially within my Personality object:

class Personality {      // Array of Personality objects     public $personalities = array();  } 
like image 954
Luke Avatar asked Sep 13 '12 15:09

Luke


People also ask

Can I use foreach on Object in PHP?

PHP provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.

Which is faster for loop or foreach in PHP?

The foreach loop is considered to be much better in performance to that of the generic for loop. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.

Can we use break in foreach PHP?

To terminate the control from any loop we need to use break keyword. The break keyword is used to end the execution of current for, foreach, while, do-while or switch structure.


2 Answers

It much depends on the IDE you are using.

In Netbeans and IntelliJ you are able to use @var in a comment:

/* @var $variable ClassName */ $variable-> 

The IDE will now know that $variable is of the class ClassName and hint after the ->.

You can try it out in your own IDE as well.

You can also create a @return annotation in a getPersonalities() method stating that the return will be a ClassName[], which means an array of ClassName objects:

/**  * Returns a list of Personality objects  * @return Personality[]  */ function getPersonalities() {     return $this->personalities; } 

this also depends on how your IDE is interpreting this type of hinting.

To use this in foreach loops you can do 1:

/* @var $existing_personality Personality */ foreach( $quiz_object->personalities as $existing_personality ){ } 

or 2:

foreach( $quiz_object->getPersonalities() as $existing_personality ){ } 

both should enable IDE hinting, if your IDE is kind enough.

As an extra note, if you want to use this inside it's own class, you can use the same signature when declaring a class variable:

class MyClass {       /**      * @var ClassName[] $variable List of ClassName objects.      */      var $variable;  } 
like image 79
Krycke Avatar answered Sep 22 '22 01:09

Krycke


just thought I'd throw this in there for those using phpStorm.

I found the way to get the IDE to auto-populate the methods for an object was by including a quick if check beforeheand checking that the object exists and that the $var was an instance of said object.

Example:

            foreach ($objArray as $obj) {             if (is_object($obj) && $obj instanceof DataObject) {                  $obj->thisMethodShouldBeAvailableInPHPStormNow();              } 

Found this question while searching for a better way, but the above works for me.

Cheers!

like image 41
lordvig Avatar answered Sep 25 '22 01:09

lordvig