PHP Casting Variable as Object type in foreach Loop

前端 未结 5 559

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

// Loop through each personality th         


        
相关标签:
5条回答
  • 2021-02-01 15:43

    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!

    0 讨论(0)
  • 2021-02-01 15:48

    You can always call out to a separate function from within the foreach, and declare the class in the function declaration. This might also have the benefit of letting you reuse this code elsewhere. For example inside the function getPriceFromProduct below, you see how I declare the class of $product to be Product.

    Of course I agree it would be nice to not have to do it this way but hey, it works.

    class ProductBundle {
    
      private $products; //buy this
      public function get_products() { return $this->products; }
      public function add_product($product) { $this->products[] = $product; }
    
      public function get_price() {
            $products = $this->get_products();
            $prices = array();
            foreach($products as $product) {
                $prices[] = $this->getPriceFromProduct($product);
            }
            return array_sum($prices);
        }
    
        private function getPriceFromProduct(Product $product) {
            $price = $product->get_price();
            return $price;
        }
    
    0 讨论(0)
  • 2021-02-01 15:50

    If you want actual type declarations in the code as opposed to comments that could be picked up differently depending on the IDE, you can use the array_* functions, for example array_walk.

    array_walk($quiz_object->personalities, function (Personality $p) {
        echo $existing_personality->GetQuizMakerPersonalityHTML();
    });
    
    0 讨论(0)
  • 2021-02-01 15:54

    I know this post is old but I think this may help someone:

    In PhpStorm works this way, maybe in others too.

    /**
     * @param ClassName[] $variables
     */
    public function loopFunction($variables){
        foreach ($variables as $variable) {
            echo $variable->functionName();
        }
    }
    
    0 讨论(0)
  • 2021-02-01 15:58

    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 讨论(0)
提交回复
热议问题