PHP将变量强制转换为foreach循环中的对象类型 [英] PHP Casting Variable as Object type in foreach Loop

查看:72
本文介绍了PHP将变量强制转换为foreach循环中的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中, $ quiz_object->个性包含一组个性对象。

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

如何投射(我认为这是正确的词)变量在foreach循环中 $ existing_personality 作为对象类型?

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

我希望这样做,以便在键入 $ existing_personality-> ,我会获得该对象类型可用的公共功能列表。

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

更新

此刻,Zend Studio不知道我要遍历一系列个性循环中的对象,它只是认为这是一个标准变量。但是,它是一种类型,我的代码运行良好。我只希望IDE在foreach循环中的变量上有IDE提示。

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.

为了使我很清楚,如果我有其他所有对象,这些提示也会出现: / p>

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

但是,一旦我开始循环使用foreach,Zend便无法知道我在

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.

这是在我的 Personality 对象中最初定义个性数组的方式:

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

class Personality
{

    // Array of Personality objects
    public $personalities = array();

}


推荐答案

在Netbeans和IntelliJ中,您可以在注释中使用 @var

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

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

IDE现在将知道$ variable是ClassName类,并在<$ c $之后提示c>-> 。

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

您也可以在自己的IDE中进行尝试。

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

您还可以在 getPersonalities()方法中创建 @return 批注,说明返回将是 ClassName [] ,表示ClassName对象的数组:

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;
}

这还取决于您的IDE如何解释这种类型的提示。

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

要在foreach循环中使用它,您可以执行1:

To use this in foreach loops you can do 1:

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

或2:

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

如果您的IDE足够好,都应该启用IDE提示。

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;

}

这篇关于PHP将变量强制转换为foreach循环中的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆