IBM delves into what’s new in PHP 5.3: Part-1 shows you the changes to the object-orientated capabilities, and Part-2 shows you the exciting new possibilities with real closures and lambda functions.
[“Read more” for Kroc’s personal commentary]PHP is a language I dislike, but am good at. It is a language that, as long as you stay away from certain parts of it, you’ll be safe and it works well. It can even be beautiful in some rare instances.
The very hackish nature of PHP does allow for rapid development, but it’s eclectic design makes it a minefield for anybody who isn’t personally motivated and regimented about clean, safe and sensible coding.
The new improvements in 5.3 only continue the junk-shop eccentricity of the language. Great when you know you want these things and how to use them, a pain if it’s all new to you.
PHP can claim to be “Object Orientated” until the cows come home, but the fact of the matter is PHP’s native objects and commands are still painfully procedural, trapped in a stasis field of backwards-compatibility.
Whilst a new high-speed fixed array is nice, the project is trying to solve it’s problems by bundling a library rather than entering this stuff into the core syntax. I honestly don’t want to use a bolted-on library and to have to instantiate classes for something I would much rather be able to do properly inline with the native syntax array (...)
. Having to use a library makes for syntaxically unpretty and elongated code, the spawning of more and more one-shot classes and variables until your code starts looking like VB6 meta-programming.
Closures:
The most beneficial, useful and almost elegant addition though is closures. The ability to create inline functions like Javascript. It’s messy, it’s confusing for beginners, it’s hackish — but like Javascript, it makes for rapid development, rapid prototyping and rapid rewriting.
PHP currently can create lambda functions via create_function
, but this accepts the function code only as a string, meaning that it’s not practical for anything more than one or two lines of code as you have to escape the string. Text editors can’t syntax highlight this or provide any additional interactivity with this horrible function-text-blob.
Closures change everything. Now it is possible to write array_map
iterators inline, of any length, as anonymous functions! This brings the sort of power Javascript developers have with Prototype and jQuery right into the PHP syntax.
Closures alone is going to make a massive difference to the way I code PHP. It’s going to allow for much tighter code across the board. Developers will be able to use a lot less ancillary and pseudo-private functions polluting the global namespace.
Final Thoughts:
PHP is looked down upon in many circles. It’s the ugly duckling, but with all the popularity. Javascript’s terrible lack of rigour and overal hackish nature is not looked down upon, instead it’s adored as the language of web 2.0. The roll out of PHP 5.3 will finally give PHP a swing away from “doing things right” as far as Ruby and Java are concerned, and toward the more quirky, rapid, web 2.0 style of Javascript development.
PHP already suits Javascript developers well, now it really will suit them down to the ground.
The writeup makes an interesting observation, pointing out how PHP is often criticized for things that Javascript is lauded for.
I don’t really understand why there’s so much disdain for PHP. Granted, I wouldn’t want to try writing a desktop GUI app with it – but I wouldn’t want do that with BASH scripting either.
“I don’t really understand why there’s so much disdain for PHP. Granted, I wouldn’t want to try writing a desktop GUI app with it – but I wouldn’t want do that with BASH scripting either.”
The disdain, I am guessing, is that too many people try to use it for things it is not designed for. PHP is a server side scripting language for the web. The main purpose when it was designed was to do things you would otherwise need cgi-bin access for, like page counters, etc. It is not for nor intended to be used for writing a desktop GUI application. JavaScript is better suited to that task as it is a client side language, and the code is executed on the local machine instead of on the server. Each has it’s own purpose. Too many people try to take one or the other and do everything with it, then bitch about it when it does not work or is too hard to do what they wanted to do. If the right tool was picked in the beginning, this would rarely be an issue.
And if I’m going to write a Desktop GUI application, I’ll write for the platform I’m developing on and use their native Frameworks, none of which are Javascript. Both PHP and Javascript lose their value when they try to play the role of other languages; and completely lose credibility when they proclaim themselves better choices.
“And if I’m going to write a Desktop GUI application, I’ll write for the platform I’m developing on and use their native Frameworks, none of which are Javascript. Both PHP and Javascript lose their value when they try to play the role of other languages; and completely lose credibility when they proclaim themselves better choices.”
I agree. My wording was off a bit, though pretty much meant the same thing.
Languages don’t have credibility. They’re tools. Just cause a monkey wrench sucks for cutting a piece of lumber doesn’t mean it “loses all its credibility”. Maybe the manufacturer loses credibility. Maybe the person recommending monkey wrenches for cutting wood loses credibility. But not the tool, its still great for ‘fixing’ monkeys and smashing walnuts into a gazillion pieces.
Seriously, if anyone proclaims php or javascript to be a better gui builder than native frameworks. They lose credibility.
I had long been curious if there was some argument against PHP that was obvious only to more-experienced coders.
But I had the general impression that you outlined – it does seem that many of the anti-PHP complaints comes from people who are trying to use it in situations where it’s not the right tool for the job.
Can you point to examples of this? While I realize that the php interpreter can run standalone on a desktop box, I hardly think you can blame PHP’s reputation on the vanishingly small number of cases where someone actually tries to use it that way.
PHP has a bad reputation bacause it has traditionally dumped hundreds of identifiers into a single name space, and eschewed objects (among other things). Until, of course, it got superficial bolt-on name spaces and superficial bolt-on object orientation to cover those bullet points. PHP has been the very archetype of the negative things people mean when they derisively refer to “scripting languages”.
Personally, I think that PHP has what defenders it does because a lot of people got their programming start with it and really buy into the marketing about it now being object oriented.
PHP has the reputation it has because it was designed, as you say, for web page hit counters, etc. and then people started trying to write large and complex *server-side* applications because it was all they had, or at least they thought it was. And while successive versions have addressed some of the buzz word bullet points, it *still* sucks for writing real applications. For real work, use Django or RoR instead. Or Java, if you’re into that and don’t care about carpal tunnel syndrome.
Edited 2008-12-21 20:32 UTC
“PHP has the reputation it has because it was designed, as you say, for web page hit counters, etc. and then people started trying to write large and complex *server-side* applications because it was all they had, or at least they thought it was. And while successive versions have addressed some of the buzz word bullet points, it *still* sucks for writing real applications. For real work, use Django or RoR instead. Or Java, if you’re into that and don’t care about carpal tunnel syndrome.”
Well, it was not all they had, but it was easier to use than perl, especially if your host did not provide you cgi-bin access, which not all did back then. RoR and Django came late to the party, with PHP being in use since 1995, and RoR and Django not showing up until 2005. Very few companies or developers will rewrite applications that work just because a new language is out.
My recollection is that Perl/cgi became popular before PHP did. And PHP vs Perl was one contest that PHP, bad as it is and was, could rightfully win in the mid to late 1990s. PHP took the dynamic web from Perl back then and hasn’t relinquished it to anyone to this day. Users of Django, RoR, and Tapestry represent an elite minority of folks who are well informed enough not to go with the PHP herd.
Well I reckon PHP is quite an attractive language. I haven’t studied PHP before but I have looked at the syntax and have played with it so if someone gives me an existing PHP file, I will be able to modify it and customize it to suit my own needs. The language seems quite easy to learn. The syntax is similar to C++. I believe PHP is the future of server-side scritping. ASP, I don’t know who is using ASP (except may be The Inquirer) but pretty much every site I’ve gone to is using PHP.
> PHP can claim to be “Object Orientated” until the cows come home,
> but the fact of the matter is PHP’s native objects and commands
> are still painfully procedural
Sigh. I think you’re confusing PHP4 with PHP5. PHP5 *is* objected oriented, just as C++ is.
PHP5’s object model was based on Java’s and contains more object reflection and method interception
features than either C++ or Java (though less than Python and Ruby). The fact that you can program
nonobjected oriented and nonprocedurally using functions that are common to C and Perl is a plus,
not a minus. It makes training and transitioning code away from these languages to PHP easier.
The key thing lacking in PHP5 is a good object oriented alternative to functionality that’s currently
only available in functions. PEAR and the Zend Framework fill in the gaps substantially so that you can almost not have to call a single non-objected oriented function again. But neither
are “standard” in the sense that Python, Ruby, and Java have a standard library.
I write exclusively v5 for PHP. A language cannot be properly object-orientated if the base objects are themselves not objects. String, Array &c. are not object-orientated and any commands involving them are entirely procedural. Using libraries to dance around this doesn’t change what PHP inherently is—a procedural language with objects tacked on.
I do understand what you’re saying, but having coded a lot of VB6, Javascript and PHP I know the limitations of hybrid languages.
Don’t know why you’re getting marked down, other than the fact those marking you down don’t know a damn thing about OOA/OOD, let alone OO Programming.
We can argue semantics until the cows come home, care to elaborate on the limitations of the hybrid languages?
For my $.02, I’d prefer if they went to a perl6 style type system where you can declare an int as an int, so you could have an array element take less than 50 bytes. Rather than have int be an object.
I’ve been coding in PHP v5 for sometime now. On a large scale application in fact. During this time, I’ve experimented extensively with the language: I’ve gotten various forms of multiple inheritance, before traits came out, meta-programming with annotations, and a ton of other crazy features hacked up. All of that involved heavy use of magic methods (__call, __get, __set), and the rest of PHP’s object system.
Just to make it clear, those are crap. You can’t use them to satisfy interfaces or abstract methods of any sort. You don’t even have options around point of entry, it’s always last! PHP in my experience, is a poor, thin veneer atop C. At the same time, I’ve pushed it pretty far and it’s kept up, sort of.
And then there is type hint, which is balls, it’s great to help IDEs provide code completion, but in such a loosely typed language you’d think you’d be able to create juggling semantics for user defined types, otherwise you lose a lot of the dynamic nature of the language as you’re shoe-horning a crap “static” type system.
What other type of commentary than personal is there from a private person Kroc? Official commentary from Kroc Industries?
Sorry for offtopic, I just don’t like redundancy. Adding unnecessary words to anything makes it sound more important than it actually is. And that gets on my tits in a huge way. Nothing personal. :]
Yeah. “Female lesbian” and “adult woman” are both redundant. Though technically a woman could be a human female of any age, the default setting is already adult. It’s only when you add “young” or “little” that non-adults come to mind.
Yet, strangely enough we say things like “ATM machine” and “PIN number” without thinking.
Hmm.
Color me weird, but EVERY time I come across “PIN number” or “ATM machine”, I spasm visibly. Even worse: “We have new DVD’s”, “I am loosing my mind”, and an all-time classic, “That’s just rediculous”.
One of the major complaints about PHP is that it doesn’t constrain the developer enough and it allows him/her too much freedom to screw up. It’s true that first, when you start coding in PHP you do the most horrendous mistakes (I know I did), but after one or two years worth of experience the speed and flexibility of PHP coding is certainly a blessing.
The second major complaint about PHP is about OOP. I don’t want to sound like trolling or maybe I did something wrong all this years, but I’ve never felt the need for OOP and I’ve made some pretty big and complex websites, that work well and have vast amounts of traffic.
PHP is really the best tool for web programming on the server side … use it just for that and everything will be alright.
Your reviewer (and both IBM articles) fail to mention the new Unicode handling extension in 5.3 which is based on ICU (http://www.icu-project.org/, http://www.php.net/manual/en/book.intl.php). Fancy pants language gimmicks always get the fan-bhoys up in arms, but as a PHP pro I’d say that this Unicode stuff will improve the platform much more. Just think, even OSNews might be able to sort out their character encoding issues 😉
For the last year or two I’ve had my site ran with php compiled with –enable-zend-multibyte and the rest of the stack (http, db, text files, …) in UTF-8. Naturally it doesn’t solve direction ltr and rtl issues, but I never have to bother about encodings.
I don’t see the purpose of this unicode extension, to be honest, apart from perhaps solving 3.141’593 vs 3,141593 issues.
“PHP has the reputation it has because it was designed, as you say, for web page hit counters, etc. and then people started trying to write large and complex *server-side* applications because it was all they had, or at least they thought it was. And while successive versions have addressed some of the buzz word bullet points, it *still* sucks for writing real applications. For real work, use Django or RoR instead. Or Java, if you’re into that and don’t care about carpal tunnel syndrome.”
Have you used CodeIgniter or CakePHP? CI in particular is an excellent PHP development framework that can easily compete with the likes of Rails. When using a proper framework like CI you get first class code, security, and a wealth of libraries that, on top of PHP’s already rich library, provides a very good RAD environment for the web.
People always compare PHP to Rails which is an unfair comparison. PHP is a language, Rails is a framework. If you want to compare oranges and oranges, compare CodeIgniter with Rails, or PHP with Ruby.
Edited 2008-12-22 02:56 UTC
I’ve written in PHP. I’ve written in Python. I’ve written in TurboGears and Django on top of Python. And I’ve written in RoR on top of Ruby. I know the difference between a language and a framework.
With CodeIgniter and CakeWalk, would one still be writing in and on top of PHP? If so, you are missing the point. Look at it this way. If I told you that there was this fantastic new web framework out there called Q-Igniter that was based on qbasic that you just had to try… how seriously would you take that?
My web programming started in PHP, but I’ve since moved on to real languages.
Edited 2008-12-22 03:20 UTC
“My web programming started in PHP, but I’ve since moved on to real languages.”
You do realise that comments like that constitute serious trolling right?
PHP is a serious language and IMHO you’re being a little self righteous. PHP can and is used to build real web apps every day – that’s a fact. It’s fast, flexible, robust, etc, and I for one prefer it over Python.
Please take a look at CI (http://codeigniter.com/) and at least give it a fair review before making statements like the one you just made.
We use PHP5 on 90% of our websites, Ruby 2% and the rest in C# or Python.
I really like PHP5 for the most part.
Side note: When people talk about “real languages” what are they referring to? Ruby is written in C as are most of the scripting languages (C or C++).
I like Code Igniter over Zend.
Edited 2008-12-22 14:11 UTC
Fine with me. Whatever you say. In the end, It’s probably good that so many people are still clinging to PHP. It gives people who don’t just stick to the first thing they learned a well deserved edge.
Building a house (framework) on top of a poor foundation doesn’t change the poor foundation. PHP’s supposed “object orientation” was bolted on in 5.0. Its namespace support was bolted on in *this* release. It’s 2008, and namespaces are *new* in this release of PHP (if you can believe it). Until now, *everything* has lived in the (one and only) global namespace. How can anyone who has worked in anything else call that a real language? PHP, an acronym for Personal Home Page, was never designed for the kinds of applications that are common today. Languages that were designed with larger projects in mind, and have the required features built into their foundations, rather that attached with sheet metal screws, are simply better suited to today’s web.
I’d be interested in the details of your web development experiences in other languages and frameworks. Java/Tapestry? Python/Django? Ruby/Rails? Perl/Catalyst? Others? Compare and contrast them with your PHP/Codeigniter experiences? You do have actual web development experience with languages and frameworks other than PHP ones, right?
Edited 2008-12-22 15:57 UTC
You do realise that PHP is being used in some of the biggest websites in thw world (Yahoo, Facebook, and many many others) don’t you?
And these organizations hire elite software engineers, who are usually given carte blanche to choose the language/framework/platform to build whatever offering they have, and make it work, and be competitive, and present a compelling product.
And quite often, the choice is PHP, for various reasons.
In fact, a few years back there was a long online article that presented the process Yahoo engineers went through in choosing a scripting language to replace their previous in-house proprietary scripting language (which was getting unwieldly to maintain), and why they chose PHP.
Of course, PHP has it’s warts, lack of proper namespacing is one of them (until recently), just to name one. But guess what – it works, and enables people to create great websites.
Personally, if I were implementing a greenfield website, I would take a long look at the following:
Rails
Django
Grails
CakePHP
… and others.
I would honestly look at all the plusses and minuses of each, including the language itself.
But I would certainly not take an elitist stance and automatically dismiss one or the other due to trolling I read on message boards.
Zend is where the smart money is going. Its more flexible. But maybe that’s just me. I don’t like most frameworks ( like ROR).
Kroc,
While I concur with most of your analysis of (and love/hate relationship with) PHP, I am a little surprised that you would compare PHP to “Javascript’s terrible lack of rigour and overal hackish nature”.
I don’t see a Javascript as being hacked together in that way at all. Rather, the progress of Javascript seems much more formalized and thoughtful than in most other scripting languages out there (there is actually a formal specification). Yes, Javascript is very simple, and doesn’t attempt the sorts of things you find in big systems languages, but it is an elegant simplicity, with only a few truly ugly design choices that I can think of.
And the work going on to develop the next-generation Javascript looks *very* promising.
Javascript suffers a bit from premature standardization. Netscape had a browser war to fight. But you are correct. It was not just hacked together as a quick and dirty scripting language. Javascript is a widely misunderstood language. And one of the things that is most widely misunderstood is that it is most definitely *not* very simple. The reference I use is O’Reilly’s “Javascript: The Definitive Guide” which weighs in at a thousand pages.
It has its bad points, like weak typing. (I should be very clear that I far prefer dynamic typing over static typing. But weak typing is just *too* error prone.) Rather bizarrely implemented weak typing, at that. 1 + “1” = “11”?
Javascript has also suffered from a combination of being given a name that makes it *sound* flimsy, and the fact that implementations up until the currently emerging crop of them has *actually been* flimsy. In addition, the way “web developers” pass around Javascript snippets in cookbook fashion hasn’t helped its image, either.
But the language standard itself is solid. And that’s good. Because we’re all going to be using a lot more of it in the coming years.
Edited 2008-12-22 16:43 UTC
Javascript was invented almost in an evening by one Netscape engineer. It was a hack from the word go.
Not that it’s a bad language – but don’t be fooled by the standardisation being done with ECMA Script.
The future of Javascript might look good, but it still has fundamental security flaws by design that will take eons to iron out.
What are the fundamental security flaws?
Any code from any third party (known or otherwise) has every right as code from the same domain.
There is zero security granularity in Javascript. Or haven’t you noticed the constant XSS attacks out there?
Is that really Javascript the language’s fault? Or is it the browsers fault?
There are some things that Javascript gets blamed for that really is due to other things like the verbosity of the DOM. I’m not really sure if the cross domain issue is really javascripts fault or the browsers. What other web client language is there that does have security granularity to prevent XSS?
I think there should be a code signing mechanism, similar to the ones in place for mobile platforms.
If I am understanding his claim correctly (which is worded a bit ambiguously), it is false as Javascript is actually implemented in the real world. Browsers implement restrictions upon Javascript code based upon the domain that it came from. For example, a script from one domain can only close browser windows that it opened itself. And a script cannot read the contents of documents loaded from different servers than the document that contains the script, or register event listeners on documents from different servers. See “Same Origin Policy”:
http://en.wikipedia.org/wiki/Same_origin_policy
I don’t particularly love Javascript, and personally think that my language of choice would’ve been better. 😉 But… for all its faults, it’s not quite as bad as Kroc claims.
Edited 2008-12-22 20:31 UTC
I think you mean ‘conceived of’, and I suspect every actually usable programming language out there was conceived of by a single person on a particular evening. What Brendan Eich *wanted* to do was more along the lines of Lisp, but that wasn’t politically acceptable, so yes it was a compromise of C-style syntax with functional/prototype capabilities. I think it was an inspired choice, compared to the meandering mess of most scripting languages.
Of course Javascript has security problems, but they will not take eons to iron out. Mainly, what’s required is the ability to freeze the function/prototype relationship, (or classes, as will probably happen in Javascript 2). (It would also perhaps be appropriate to think about the fact that Javascript has had a completely different security problem to solve than most other languages)
It’s not that I think Javascript is the best thing in the world, but it is a far cry from the slapped-together nature of PHP, and I think it has a more promising future.
I think it is pretty amazing (in a way) that Lisp (which stands for LISt Processor, and actually predates COBOL (which was released in 1959 (and incidentally was one of the more notable achievements of Grace Hopper (who was an Admiral (in the Navy (which was particularly unusual for a woman at that time)))) has had such an influence upon modern languages (aside from those which descend from ALGOL (which was also contemporaneous to Lisp (and COBOL, of course (though Cobol never had as much influence (although it is the one of that trio which has survived to this day (in the sense that COBOL code is still being actively developed (particularly for commercial accounting applications (even though most of the younger folks today probably do not realize it. (Fortran, too, BTW.)))))))))) (Come to think of it, though, Java is pretty much the Object Oriented COBOL of the 21st century. (Too bad about all those curly brackets and semicolons, though.))).
Edited 2008-12-22 21:39 UTC
Why can’t people just leave these technology blunders to die in peace. I mean, if you must reanimate a corpse, why do you have to select one that was retarded when it was alive.
Not retarded. Just Abbey Normal. 😉
“Fine with me. Whatever you say. In the end, It’s probably good that so many people are still clinging to PHP. It gives people who don’t just stick to the first thing they learned a well deserved edge.”
My goodness, you are keen to judge others aren’t you? I really shouldn’t respond as you’re obviously hell bent on being offensive, but…
The first thing I learned was C, thanks for asking. I’ve since learned and used commercially Java, Python, PHP, C#, VB, VB.Net, HB++, Javascript, and others. IMHO all of these languages have a place (well, not VB so much). I have a masters degree and 8 years experience as a software developer, and for all that I still believe PHP has a legitimate place in web application development. That’s my opinion and also that of many web design firms, judging by the number of job advertisements wanting PHP skills.
The PHP devs have done and continue to do a wonderful job with the development of the language, and I for one thank them for their hard work.