3 ms·
I think PHP's problems are actually quite solvable without core language changes. The OO model is actually pretty good, there's even reflection functionality th
by Joeri 14y ago
I think PHP's problems are actually quite solvable without core language changes. The OO model is actually pretty good, there's even reflection functionality that's sufficient to do stuff like auto-generating docs and self-validating DTO objects. The issue are the "legacy" api's, and the primitive types. What's missing are object api's wrapping the primitive and legacy functionalities. Those could be built quite easily though.
You could have something like jQuery's $ function, but for PHP variables, that gives you an OO api on top of that value (especially now that you can use the -> operator on return values of functions without intermediate assignment). I wonder why nobody's built that yet. I suppose because those in the PHP community are all busy programming instead of meta-programming.
- whichdan 14y agoDo you mean something like this? function s($string) { return new String($string); } class String { public $string; public function __construct($string) { $this->string = $string; } public function __toString() { return $this->string; } // several helper methods like the following: public function toLower() { $this->string = strtolower($this->string); return $this; } // this would create lots of extra objects, // but leave the original unchanged public function alternateToLower() { return new String(strtolower($this->string)); } } $username = s('ExampleUser'); echo $username->toLower(); My concern would be the memory used to create several dozen extra objects. If this is indeed what you're talking about, is there any strong benefit besides convenience?
- zeroonetwothree 14y agoOne reason to abstract away strings is that you can gain security. For example, your template might only accept an HTMLString, which can only be created from a String by escaping. Your database library might only accept a QueryEscapedString, and so on. Of course all of this is possible without creating a wrapper for vanilla strings.
- Joeri 14y agoYes, lots of memory, much worse performance, but it doesn't really matter. If you care that much about those, you're not using any interpreted language. Besides, most PHP code is linear without much looping other than iterating database records. The overhead of such an approach wouldn't matter that much, and when it does you can always use the old way of doing things. But you wouldn't need to create an object per call, you could reuse a singleton that gets initialized by a wrapper function to work on your current variable (using references). Fix($str)->replace("foo", "bar") is a syntax that doesn't require object instantiation or string copying. And the singleton could use the mb functions internally to be multibyte aware.
- Ralith 14y ago> PHP's problems are actually quite solvable without core language changes How do you fix weak typing?
- 10098 14y agoOstensibly, by writing lots and lots and lots of unit tests. In practice, however, no one has time to write or update unit tests. Which is bad, but understandable, because circumstances can be pressing. In that case, the only fix for weak typing is praying that nothing blows up :-)
- Ralith 14y agoWhile I appreciate that your post is mostly jest, it's worth noting that a "fix" that places an immense amount of additional load on the programmer is not worthy of the title (in part due to the consequences you describe). And even then, even the best unit tests rarely cover every case.
- Joeri 14y agoJavascript has weaker typing than PHP (in php you can have typed parameters for object parameters), but javascript is the new hotness and php gets no love. Typing is not the issue that makes php so ugly. Also, i write php all day and i almost never run into bugs due to typing. You adopt some habits like always casting before use, and use an ide that warns you when you're doing stuff like using uninitialized variables (phpstorm is awesome), and the whole issue goes away for 95%. I'm not saying it doesn't cause bugs, just that i've found it to not be that big of a deal.