Going Dynamic with PHP
Five-Oh writes to tell us that IBM DeveloperWorks has an interesting article about the OO advantages of PHP V's new features. From the article: "PHP V5's new object-oriented programming features have raised the level of functionality in this popular language significantly. Learn how to use the dynamic features of PHP V5 to create objects that bend to fit your needs."
- Class constants must be string literals and only string literals (no variables, arrays or objects).
- Type-hinting is confined to arrays and objects (feature?).
The unadorned output of phpDocumentor, PHP's analog to JavaDoc, is also suboptimal; for documenting PHP, therefore, go Doxygen.The answer would seem to be that there is no input validation or output interpretation going on in this sample. So, you can either write a switch block that makes sure your integers are in valid ranges, etc. or go back to individual properties. (Six dozen of one, half of the other.)
It's a huge step forward for OO development in PHP.
BUT it's still got all the crud of PHP4. For those transitioning from PHP4 objects one great feature is the new warning when using the older style of classes. However all of those things people find quirky about PHP4 still exist. For example, now you can force a function parameter to be a certain type of object, but not a basic type. You still can't even fully overload a function.
My view is that it's two steps forward and one step back. They need to consider deprecating features and making a php.ini option to not allow the use of any deprecated features.
Developers: We can use your help.
This page actually has alot of links of changes and new features. What's New
-- Brought to you by Carl's JR
There's a good summary on Zend: http://www.zend.com/php5/andi-book-excerpt.php
Basically, PHP 5 adds proper object support (think Java-style) including iterators for objects, and new extensions add good XML support, SOAP, SQLite, better MySQL support (prepared statements, OO interface, etc.)
I'd recommend reading Adam Trachtenberg's book Upgrading to PHP 5 if you're familiar with PHP 4.
I'm a fan of using objects in the right place .. but to suggest they increase the functionality of a language is simply wrong. They allow for better (well, different) organisation of code, easier reuse, and improved encapsulation over procedural or functional coding styles, but they don't actually allow you to do anything that can't be done using any other approach. The functionality of the language remains the same.
http://twitter.com/onion2k
I'd love to take advantage of some of the PHP5 features. However, most hosting services are still stuck on PHP4. How long has it been now? I am the project manager for WebCalendar, and just like during the transition from PHP3 to PHP4, it's going to be some time before we can drop "legacy" support for PHP4 and take advantage of the cool new features of PHP5. So, for now, WebCalendar and other open source apps will have to stick to PHP4.= rss1
FYI.... PHP developer articles updated daily:
http://www.devpointer.net/browse.php?l=p&t1=1
RSS:
http://www.devpointer.net/browse.php?l=p&t1=1&fmt
http://www.k5n.us
This is what I was thinking the entire time I was reading the article. I mean, it's one thing to have to whip up some small project for yourself, it's another to build a project that is maintainable by a group of people.
I'd bet that Brian W. Kernighan and Rob Pike (The Practice of Programming) would probably recommend against using it. It doesn't provide for clarity, nor does it simplify, it just makes things "easier" for the guy that writes the original code.
Notes From Under *nix: blas.phemo.us
PHP5's object are passed via reference by default. Public/Private function and variables in classes are also now supported. The language as a whole has better OOP support and the underlying engine is alot faster as well.
Also, some deprecated functionallity has finally been dropped. All in all, I've been using PHP5 for awhile now and everything works alot faster as a whole (as I use it with Apache 2 as well).
One note that might concern you, PHP5 does not come with MySQL built in anymore. You either have to download the update or compile it yourself. It's nice for those who want to use something aside from MySQL and don't want to have to keep it the module loaded constantly but it's also a pain for the beginner hobbyist who has never had to deal with installing the MySQL module for PHP.
This is my sig. There are many like it but this one is mine.
So, it's got some of the features of Ruby now, plus a whole lot of crap dragged in from PHP 3 and 4 inluding that crazy mishmash of a function library? Boy, sign me up.
"welcome to 1967".
http://en.wikipedia.org/wiki/Simula
Y
Oh, but everyone (and everything) can code perl - the trick is to hit the keys in a sufficiently random manner.There. That's either a full-featured web-browser, or a worm which will destroy the entire internet. I don't really dare to run it...
Tedious Bloggy Stuff - hooray?
I've clicked to another tab to browse at some other site and then I've seen suddently:
...as the title of the Slashdot tab. It gave me the creeps until I remembered whats the article about. Phew.
"Slashdot | Going Dynamic with PHP"
It takes a man to suffer ignorance and smile
Be yourself no matter what they say
Can anybody else read this comment? My browser keeps crashing..
Any fool can code Perl, just like any fool can code PHP/C/Java/VB/Smalltalk/COBOL etc. etc.
Anyone can write code, but very few can write really great code, that reduces the problem to the essential elements and uses the simplest approach to the problem, with the tool (i.e. language) in hand.
You can write shit code in ANY language. You can also write good code in ANY language (within the limitations of the language).
What you're saying is like "Spanish for People who can't speak in German".
It's nonsense and insulting at the same time.
You need to express yourself or solve the problem within the framework of the language you have.
You might choose a different language for a particular task, but if the language is a given, a good poet (or programmer) will make the best of it.
A better language doesn't make one a better programmer (or poet).
Or actually, the language for people who want a specific tool for a specific job. What is it with this tedious Perl clique on Slashdot? There are, I presume, plenty of other language-based cliques on /. , however the most vocal ones always seem to be the Perl-mongers. They're the ones who seem to pipe-up the most regardless of topic.
And, for some reason, I'm always left with this mental image of a tall, pasty elder geek sitting in that proverbial Mom's Basement, pulling on his long pointy beard whilst his Gentoo box spends it's 28th hour compiling a boot-loader or something, smirking as he sees his, ahem, 'Perl of Wisdom' slide down the Slashdot page like a turd in summer.
I just fucking hate you, why bother posting at all?
Perl: The Language for People Who Can't Be Arsed to Investigate the Right Language For The Task At Hand.
Perl: 'Cos You Never Really Moved On.
Perl: Why Use the Standalone Screwdriver When There Is A More Fiddly One Attached To This 52-Way Swiss Army Knife?
* Retrieves dummy and calms down *
Perl is another alternative, and admittedly a pretty popular installation (I imagine anywhere that offers PHP hosting also offers Perl) -- but for someone like me who wants to do the occasional scripting the language is not exactly ideal -- nor is it especially easy to read someone else's code. I think Perl developers are incidentally the most "guilty" party of poorly commented code in FOSS projects, which doesn't help matters.
As a designer and occasional scripter I was interested in learning more PHP at one time, but now I feel as though it's a bit of a dead end, especially for "bigger" projects. Learning Python seems to be time better spent at this point; I can run a native interpreter as well as Java and .net based interpreters to handle more "enterprise-sized" projects; Python has a stronger OO foundation than PHP in existing versions, is designed to easily integrate with C modules, and reads easily. It's also shown itself to be equal to a broad spectrum of applications from commercial tax forms software (QuickTax) to web application frameworks (Zope) to HTPC frontends (Freevo) to P2P software (BitTorrent).
As for PHP, roll me over when version 5 is standard across the board and I'll consider taking another look at it.
No, they are like completely different. About the only thing they have in common is $ in front of scalar variable names, some is-it-a-string-or-is-it-a-number situations, and C-style loops and if statements.
For the beginner/hobbyist I found LAMPP, more specifically XAMPP, to be very easy to use. The newest version came with PHP5 and mysql 4.1 (i think). It installs in minutes and works on linux and windows.
There is an implementation of this idea (and more) in PEAR's DB_DataObject package:
- dataobject.php
http://pear.php.net/manual/en/package.database.db
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{q *=2)+=$f=!fork;map{$P=$P[$f^ord_ }=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[ P.]/&&
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($
($p{$_})&6];$p{$
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
Your comment is true, but the same can be said about Perl (it just sounded like you were putting down PHP only).
This is what frameworks are for. Security and performance conscious people like myself write them so that other neophytes and developers alike can make good PHP apps without worrying about those things.
'Yes, firefox is indeed greater than women. Can women block pops up for you? No. Can Firefox show you naked women? Yes.'
PHP5 uses MySQLi which has both a procedural and object-oriented interface, and it supports features from MySQL 4 and 5 such as transactions and stored procedures. The old MySQL libraries are technically for MySQL 3, so there's not much need for those anymore.
Then there are PHP Data Objects for a unified database interface (although it is a bit primitive when compared to PEAR DB and other DBIs).
'Yes, firefox is indeed greater than women. Can women block pops up for you? No. Can Firefox show you naked women? Yes.'
I've almost never seen OO been put to good use with PHP. It's used exclusively as a tool for the developer to write, as the author puts it, "more maintainable code". However, with the (lack of) real complexity and sprawling code bases which accounts for most web sites, it just adds complexity by adding a "system" for the developer.
Forgetting the developer, it adds nothing, and has a major impact on speed and memory. It adds nothing as in 99,9% of the times I've seen it used, it's 1) stateless and 2) a collection of single object instances (one page, one database connection, one user, etc.).
In a lot of cases, I think the programmers would have been a lot better of just seperating the logic in functions and few files, and then he can understand his own code after a few months off it.
Not enforcing Object Oriented programming is actually a very good thing, one of the few good things in PHP5.
Giving the option to use OOP (with a good object system, which PHP doesn't have) is good, forcing it on the poor user and preventing him to write as much as a line of code outside of a damn class is stupid, and is a god damn failure of both Java and C#.
While OOP is a good idea for some problem, others are better solved using more imperative or functional styles. That's why I much prefer Ruby or Python to Java: while they have great object models, they don't try to beat you with an ugly stick if you don't wrap every damn thing in a useless class that is only here because the language absolutely forbids you from doing otherwise.
"The way we can tell it's C# instead of Haskell is because it's nine lines instead of two." -- wadler
You mean kinda like every language that supports OO design? None of this is even remotely ruby specific, or even ruby inspired.
They are always upset when anyone chooses any other language for any purpose, because, in any touted feature of any language, you can do exactly the same thing almost exactly the same way in Perl, but better. That is not sarcasm, I am not being ironic, it is flatly true. Often you can literally cut-and-paste the code.
Other languages have an easy way to do something, and a correct way to do something, and a stupid way to do something you get from newbies. You can usually figure out what any line talking about in thirty seconds once you grasp the syntax. (Which, admittedly, can take a bit of time, like with COBOL and Lisp, for examples of languages with semi-odd syntaxes that are not in any other way alike.)
Perl has all those ways, and one hundred others. This is not an exaggeration. You can write BASIC in Perl, you can write PHP in Perl, you can write C in Perl, you can write Lisp in Perl, you can write C++ in Perl.
You sit two programmers of a medium skill level and have them write asm, and they will write identical code. You have them write C, and they will write near-identical code. You have them write PHP, and they will be roughly the same. You have them write Perl, and sometimes one of them will come up with at least one line that the other doesn't immediately recognize, because one of them basically writes PHP in Perl, and the other basically writes C++ in Perl.
This isn't limited to Perl. Programmers often write C in C++, or PHP 4 in PHP 5. When languages get new, better ways to do something, you end up with an 'old style' and a 'new style' and people can upgrade the language itself without upgrading the style they write in. Other people work on their codebase and put in 'new' stuff, and the old guys are baffled.
Perl is merely the only language that has ever deliberately done this. Perl is like being able to write in any programming style you want, at any time, using a semi-consistent syntax, along with features that no other language likes, like $_. Perl fanatics are under the mistaken impression this is somehow a feature, as opposed to making it fucking impossible to ever read any Perl code unless it happens to be written in the style you know.
If corporations are people, aren't stockholders guilty of slavery?
You're really overreacting mate, especially since the kind of english being put down is the sort that's poor and lazy when the speaker could just as easily speak normal english. At least you included the word somehow as a handy comfort zone.
Now, how to take away the flamey tone of this reply? Common ground!
With the MySQLi extension (the 'i' stands for improved) you basically call methods on/read properties of a mysqli object rather than passing a resource to procedural functions, get a mysqli_stmt object when you prepare a statement, get a mysqli_result object back from a query, etc. It's clearer and neater than the old way and it opens up opportunities for using some of the newer features like iterators.
Sorry if it sounded like I meant that it does schema-reading magic and presents your tables to you as objects or something (although I don't doubt that there's code out there somewhere that does it, and it'd be very doable by implementing appropriate _get, _set and _call methods on your particular object. Look at the SOAP extension for an example of that sort of thing.)
Let's see how it can be done better:
My firm is a fairly big fan of WASP (http://wasp.sourceforge.net/). Check it out.