Suppose I have an array of a objects of user defined class. Wanted to know how do I extract the elements of the array in PHP.
// class definition class User { public $fname; public $lname; } // array of objects of the class defined above $objUser1 = new User(): $objUser2 = new User(): $objUser3 = new User(): $objUser4 = new User(): $alUser = array(); $alUser[] = $objUser1; $alUser[] = $objUser2; $alUser[] = $objUser3; $alUser[] = $objUser4; // trying to iterate and extract values using typcasting - this does not work, what is the alternative. foreach($alUser as $user) { $obj = (User) $user; // gives error - unexpected $user; }
Thats how I used to do in java while extracting objects from the Java ArrayList, hence thought the PHP way might be similar. Can anyone explain it.
Why do you need typecasting for this?