Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:26:28+00:00 2026-06-11T09:26:28+00:00

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

  • 0

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();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:26:29+00:00Added an answer on June 11, 2026 at 9:26 am

    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;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that populates an array (this is within a loop):
In the following code, I have verified that connecting to a URL from within
Within a for loop, I'm using the following code to convert from one date
I have an NSMutableArray with objects within it and I use the following code
I have the following code within a razor mvc3 view that is duplicated in
In the following code, I'm looking to have the panel that is within the
I have the following code which recursively operates on each element within a List
I have the following code with the if...else statement within a while loop. $colour
I have a page that is centered within the browser using the following code:
I have the following code that changes the pages from within JavaScript: var newUrl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.