8 ms·
Php-o: metaprogramming PHP to give it a saner API
- rikthevik 14y ago*facepalm
- deleted 14y ago[deleted]
- crindy 14y agoJavascript is the only language that runs in the browser. There are tons of great alternatives to PHP to run on a server.
- ergo14 14y agoThis comment implies like there aren't any other great alternatives for jquery in the browser. What about dojotoolkit or YUI library? I don't see how javascript is here to blame.
- treeface 14y agoI think the point he's making is that you don't need to find a jQuery-like library for PHP because you can just use another language. I personally say nuts to that. With all of the latest projects, PHP development is actually getting to be pretty non-shitty.
- tikhonj 14y ago"Non-shitty" is a pretty low bar though. When you have a wide variety of choices, you may as well hold out for "good" or "great", even if it means learning a new language, some new technologies or even a new mindset. Having to learn something or spend some time setting things up is a trivial cost for any non-trivial project: increasing your productivity when programming or maintaining code is a very good return on investment.
- Pitarou 14y agoNot always. I am obliged to use PHP at the moment.
- sergiosgc 14y agoUnconstructive comment. Which alternatives, and why? I don't think there's even one which is a clear cut alternative (where the downsides are minor, when compared with the upsides). On speed alone, PHP gets a huge head start when comparing alternatives. Note that I'm not saying PHP is great. It's a cesspool of language defects. However, it's success has merit.
- ergo14 14y agoOk, please use any decent php web framework and compare the speed to lets say python/ruby - not to mention something like pypy. "Hello World" is not world-like performance benchmark.
- jtreminio 14y agoWhy would you compare a PHP framework to either Python or Ruby?
- camus 14y agobecause people do not develop web-apps with bare python/ruby
- jtreminio 14y agoSorry, I don't think what you wrote makes any sense. He's comparing "any decent php web framework" to Python or Ruby, because neither of those languages are used without frameworks to develop web-apps?
- ergo14 14y agoNo, because php is rarely used to develop applications without proper level of abstraction - so it makes sense to compare apples to apples.
- ergo14 14y agoIt kind of resembles of what a wildly popular javascript lib does isn't it?
- oh_no_my_eyes 14y agothe sanest thing ive seen about php in a long time.
- gambler 14y agoMaybe, but if several people develop several APIs like this, it will mostly make everyone's lives more difficult. This should be a part of the standard distribution.
- Pitarou 14y agoThis library looks like it will make my life a lot easier. Many thanks, Joeri!
- leeoniya 14y agoi have something similar on the back-burner that i haven't pushed public yet, but only a single, one-letter function like jquery that takes any type. there are soooooo many functions to abstract. even though most are one-liners, i ran out of steam :) certainly not something to use all the time, especially in perf-oriented code, but it's nice sugar.
- lkrubner 14y agoHow good is PHP? I was thinking about peer review the other day, and how one measures peer review. One of the most important metrics is the number of citations an article acquires. If an article is published and, over the next 20 years, is only cited 3 times, we can say that it failed to influence its field, but if an article accumulates 1,000 citations, we can say that it definitely had some impact. Then I thought about PHP, and how it compared with other languages. I went to Google and started searching: influenced by gosling java influenced by hickey clojure influenced by matz ruby influenced by Stroustrup C++ influenced by larry wall perl and then finally: influenced by lerdorf php You look through some of those links and you notice a difference. A lot of people write about the brilliant things that have been said by Gosling and Hickey and Matz and Larry Wall and Stroustrup but no one ever cites Ramus Lerdorf as an inspiration, nor does anyone seem to think he has ever said anything especially brilliant or insightful. I'm sure if you dig you might find exceptions, but as a rough metric of who has influenced who, I think this reveals something important about how computer programmers perceive the quality of PHP and one of its core contributors. (You could counter-argue that PHP has several core contributors, in which case, I would counter-counter-argue by asking that you please suggest a core contributor to PHP who is quoted with the same admiration expressed for the other architects that I've listed here.)
- jtreminio 14y agoSymfony2 gets quite a bit of love here on HN... as much love as anything PHP-related can get. Its creator, Fabien Potencier, is usually cited as someone bringing sanity and quality to the PHP world.
- mylittlepony 14y agoI think some PHP devs are either shy or don't even bother with these discussions. Personally, I think Symfony2 can beat Rails any day of the week. Amateurs have a hard time finding out what is more important: syntactic sugar and "coolness"? Or architecture and getting things done? There is also the fact that lots of HN devs are stuck in the past. Just the other day someone asked "What does it feel like to develop in a language that does not have namespaces?". Seriously? That was ~4 years ago. I'm sorry if their languages don't evolve as fast as PHP does.
- chez17 14y agoAs a PHP programmer the inconsistency of the string functions is not only bothersome, it's embarrassing. This seems like an interesting fix for that.
- knieveltech 14y agoAs a PHP programmer the inconsistency of the string,array,regex,et al functions is an occasional annoyance blown way out of proportion, typically by developers who don't even use the language. The horse is dead, continuously beating it is tedious and avoids the elephant in the room, mainly that for the tasks it was invented to handle it still (despite all it's innumerable and interminably cited warts) serves more pages than any other language with no real change on the horizon. You want to gripe about something? Let's talk about Drupal's internals, or better yet, JavaScript.
- Tloewald 14y agoI think trying to cast stones at JavaScript from PHP land is foolishness, but I agree with your assessment of the PHP string function situation. This project seems to produce PHP code that is an order of magnitude uglier than ordinary PHP. (No mean feat.) Why not simply implement a nice string class wrapped around ordinary strings with consistently parameterized chainable methods? The resulting code would be cleaner and more idiomatic (in a good sense). I don't think the "win" from doing this is worth the bother (unless maybe UTF8 support were managed as well).
- knieveltech 14y agoWritten much JavaScript without the benefit of jQuery?
- plaguuuuuu 14y agojQuery seems more like an API to talk to the DOM than an extension or wrapper around Javascript itself. JS simply doesn't reach the same depths of madness and ineptitude as PHP does at the most fundamental levels of the language. Even if it's still fairly insane compared to languages that are actually kinda elegant.
- leepowers 14y agoCool, love it - the multi-byte support and baked-in validation is awesome, going to play around with this.
- daniloassis 14y agoI think this type of approach will never stick with PHP community (at least with the "serious" developers). This hurts so many principles and directions that PHP community is FINALLY diving into, like SOLID and stuff. Accepting simple things like strings are not objects is the way to improve how PHP devs build their stuff and start focusing on important things like defining de-facto libraries and joining forces to make them the best and most flexible available. Don't get me wrong, the idea is pretty good (even if it's a simple try to mask procedural PHP functions)... but it seems like a swiss knife in the end.
- notJim 14y agoI don't think this is a project that I would see being used anywhere, but I do think it introduces an interesting idea for the language's evolution. I am glad to see it here.
- hearth 14y agoAside from anything else, the function call and object instantiation overhead of this library is insane. The string comparison function provided as an example is 4 times slower than the code it's based upon, and 3 times slower than a completely equivalent multibyte-aware version.
- Mahn 14y agoLibraries like this are bound to introduce overhead, but in the javascript world you don't hear many people complain about the overhead of jQuery, and it is there. I think for most people a string comparison 4 times slower won't mean much. That's not to say it's "okay" to be slow, just saying it may not be big for those not pushing PHP to its boundaries. In fact last time I checked Symfony and Zend had a tremendous impact in terms of overhead, but you (almost) never hear negative comments on them. Heck, even something OOP is slower than the same thing written in procedural style in PHP.
- camus 14y agowell , you are right but symfony for instance has a lot of caching strategies, and is usually used with apc.
- hearth 14y agoThe example I'm talking about is the first non-trivial example in the readme - 34 lines of believable code, not an individual function of the library. The library is used about once every 3.5 lines, which feels less dense than most jQuery code I see, but that whole function runs 3 times slower than the plain MB-aware equivalent. Some of the library functions might be implemented inefficiently but from a quick glance they're nearly all very thin wrappers. Overhead from libraries like Symfony and ZF is quite acceptable since they offer much higher level features which you'd have to code yourself otherwise. However making all your code (excluding I/O) run up to 3 times slower, just to add some syntactic sugar, is insane.
- quadhome 14y agoWhat causes the overhead? Is there a way to get the sugar without the calories?
- neeleshs 14y agoHaving to go through the pain of the almost arcane APIs everyday, I appreciate this project and the author. Looks neat.
- Mahn 14y agoInteresting concept, the chainable object specially showcases well what PHP is missing by neglecting data types as objects. Makes you wonder whether it could be simplified further though; ie, combining s(), a(), o() and c() into a single one letter function that does it all, jQuery style. I think it'd have a better chance of adoption that way.
- plorkyeran 14y agoYou wouldn't be able to do the JSON stuff and the string wrapping with a single function, but otherwise that could be a good idea.
- Mahn 14y agoWhy not? You could test if the string is valid json, and if it is do object wrapping, otherwise do string wrapping. This method is used often in jQuery, figuring out what to do depending on what you pass to it. EDIT: and I guess a second, optional argument to force a specific data type could be added for those rare cases where you have a json string and you want to run string functions on it.
- tangue 14y agoJquery is a reference for designing a clean API and I suspect (this library aside) many php coders would have liked if php 6 move this way. But php core dev are waaay too conservative.
- dools 14y agoThe biggest draw card here is the multibyte string handling by default and sanitization of argument ordering in string and array functions. The object stuff, kind of started to lose me. The chainables is cool but for me I could have stopped reading at s() and a() and been happy. I think it'll definitely be worth experimenting with even if just for those two things, thanks for sharing!
- kijin 14y agoLooks neat. Although I doubt that "hacks" like this will become popular among PHP users any time soon, a proliferation of similar hacks might give the PHP core devs some ideas about how to clean up the language in the next few major versions. PHP could introduce a native object-oriented way to interact with built-in types in PHP 6, just like they did with SPL in PHP 5. Then, in PHP 7, they would deprecate the old functions, and in PHP 8, finally remove them in favor of the new style. After all, new major versions are for backwards-incompatible changes, right? The whole cycle might take a decade, but it would be worth it. Anyway, here are some suggestions. 1. "echo 'error message' and exit" isn't a particularly elegant solution, and in fact symbolizes everything that is wrong with PHP. What about throwing an exception instead? 2. The ability to specify the charset at object creation and convert to another charset later would be quite helpful, because nobody can remember all those mbstring functions. s('str', 'EUC-KR')->convert('UTF-8') maybe? 3. Why is O.php checking whether magic quotes are enabled? Since you're not even touching GET/POST variables, magic quotes have nothing to do with your library. Are you going to throw an error every time you discover suboptimal settings in the user's PHP environment? 4. Don't modify session settings until the user calls session_start(). They might not want to use your session handling functions, only your string and array functions. Simply including the script should have as few side effects as possible. This helps integration with existing apps. 5. Some of the methods that I'd really love to see in the string class are startsWith(), endsWith(), and contains(), copied straight from .NET. It sucks that I have to do strpos() === FALSE every time I want to check whether a string contains another string, or !strncmp($a, $b, strlen($a)) every time I want to check whether a string starts with another string. 6. While we're trying to clean up PHP's API, why not merge the case-sensitive and case-insensitive versions of string functions into a single method with an optional flag? This is another area where the API is terribly inconsistent, what with 'i's thrown in at random places and sometimes even 'case' to denote the case-insensitive version. EDIT: Related to 5 and 6, I wrote a similar library back in 2010 for fun, which I put online just now [1]. It doesn't use the clever iterator interface that you incorporated into your library, but I do think that my method names make more sense. [1] https://gist.github.com/kijin/4736544 https://gist.github.com/kijin/4736544
- raylu 14y agoand sometimes even 'case' to denote the case-insensitive version. To be fair, that's copied from libc (and a lot of the other naming in the string library too): http://linux.die.net/man/3/strcasestr http://linux.die.net/man/3/strcasestr
- skrebbel 14y agoI like the three one-letter functions and their effects. It's not entirely unlike how jQuery spices up DOM elements, and feels natural that way. Then, suddenly, however, there came a validation engine with annotations and reflection and whatnot. Huh? Isn't that a whole different thing? Why is this bundled with my nice little jQuery-for-PHP-scalars? It's not just that it feels like it's a different problem entirely and thus should be separate, the code feels different. Like it has a different philosophy behind it. It looks pretty nice, but why can't I use the validation engine without PHP-o? Or the other way around?
- Joeri 14y agoAuthor here. It's called an experiment for something. I'm going to productize it by separating it in the way that you propose. The validator API was ripped straight out of java, i'll see about normalizing its style.