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: 2020-04-09T11:44:45+05:30

975 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements