PHP Casting Variable as Object type in foreach Loop


This depends on the IDE that is being used. For example, Netbeans and IntelliJ can enable the usage of @var in a comment −

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

This way, the IDE would know that the ‘$variable’ is a class of the ClassName after the hint ‘->’ is encountered.

In addition, an @return annotation can be created with a method that specifies that the return type will be an array of ClassName objects. This data can be accessed using a foreach loop that fetches the values of the objects −

function get_object_type() {
   return $this->values;
}
foreach( $data_object-> values as $object_attribute ){
}

Updated on: 09-Apr-2020

584 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements