Warning when iterating with foreach over objects

PHP Tools [?] extends Visual Studio with set of advanced features to work more efficiently with PHP code.

Warning when iterating with foreach over objects

Postby uwe » November 24th, 2016, 2:05 pm

When using this Example #1 with PHP Tools for Visual Studio, it complains at the foreach loops:

Cannot use object of type 'MyClass' as array.


Obviously this is wrong, since it is an official example from PHP.

My suggestion would be to enhance the warning so that it knows a foreach loop is valid both over arrays and objects.
uwe
 
Posts: 21
Joined: October 10th, 2016, 8:39 am

Re: Warning when iterating with foreach over objects

Postby Jakub Misek » November 24th, 2016, 3:51 pm

Hi,

Thanks for the feedback.

Currently PHP Tools check whether the object implements Traversable interface so its iteration is handled by user's code.

Anyway you are right, it is possible to iterate through objects properties, but usually it is a sign of a possible error.

In a future version, we'll make this warning optional.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to PHP Tools

Who is online

Users browsing this forum: No registered users and 54 guests

cron

User Control Panel

Login

Who is online

In total there are 54 users online :: 0 registered, 0 hidden and 54 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 54 guests