3 ms·
Also the fact that he's basically reinventing "scandir" ;)
by chriswarbo 12y ago
Also the fact that he's basically reinventing "scandir" ;)
- fein 12y agoTheres an OOP way to do this now with DirectoryIterator and RecursiveDirectoryIterator. http://php.net/manual/en/class.directoryiterator.php http://php.net/manual/en/class.directoryiterator.php One of the big problems with PHP seems to be the amount of "hidden" features that you don't generally stumble upon unless you dive through the docs, or get the right google search.
- chriswarbo 12y ago> Theres an OOP way to do this now with DirectoryIterator and RecursiveDirectoryIterator. OT: Iterators aren't OO. First of all they don't work with objects/methods: we're forced to write a for/while loop, which is procedural programming. This also prevents abstraction and composition. Functions like "array_map" don't have these problems. Second, they break encapsulation since they not only expose their elements, they also expose their internal mechanisms like "next", "rewind", etc. Again, array_map and friends win here. Third, since they're a massive case of 'sequential coupling' ( http://en.wikipedia.org/wiki/Sequential_coupling http://en.wikipedia.org/wiki/Sequential_coupling ), and since objects are passed by reference, we must ensure that nothing we call in our loop body upsets the ordering (eg. by performing its own loop across the same iterator). This destroys encapsulation and modularity, and makes polymorphism and concurrency dangerous. Just because something uses the "class" keyword doesn't mean it's OO.
- skrebbel 12y agoThat's just a bad argument. Simple code that suddenly breaks because of the contents of a directory shouldn't be possible, whether or not said simple code is unnecessary because of a standard library function. The example stands, weak typing is a horrible vector for difficult-to-catch bugs.
- chriswarbo 12y ago> That's just a bad argument It's not my only argument. Here's what I wrote on the blog's own comments: > I agree with your points, but picking this line as an example of the language’s problems > is a bit of a straw man: > > “while ( ($filename = readdir($dh)) == true) $files[] = $filename;” > > Code like this smells terribly. > > First of all it uses “==”. That operator should trigger a deprecated warning, since it’s > never appropriate. Even if you want the coercion, it’s better to write it explicitly with > things like (int) or intval(). > > Next, it’s using “== true”, which is completely redundant since the “while” will interpret > the value as a boolean anyway. > > Next, it’s performing assignment inside a condition, which is generally frowned upon. > There’s potential to confuse “=” with “==” or “===”, it’s using a statement in the place of > an expression and it performs side-effects (setting the $filename value). > > After all that, it’s just duplicating existing functionality anyway: you could just say > $files = scandir($dh).