Sunday, April 27, 2008

5 PHP 5 features you can't afford to ignore

There are dozens of reasons to switch to PHP 5—not least the fact that, if you're still stuck on PHP 4, the PHP team is about to pull the rug from under your feet. Despite the fact that you may not have a choice in the matter, upgrading comes with a number of bonus new features that can help you write better code and gain access to new functionality that required a fair amount of hacking in previous version. Here's a quick list of 5 personal favourites (with a little help from my Twitter contacts):

1. SimpleXML
Although its name is the source of much frustration—simple is such a relative concept—SimpleXML is a great extension that makes manipulating XML data a breeze. Thanks to its integration with the PHP 5 object model, SXML allows immediate and intuitive access to the contents of an XML document using normal language constructs: your XML document simply becomes a hierarchy of PHP 5 objects that express elements and their attributes. Combine with XSLT for a kick-ass templating system that (a) is based on well-known standards so your development team doesn't have to learn some made-up markup language and (b) really forces you to separate your frontend from the rest of your application.

Bonus features: SXML integrates with Xpath, making complex XML data extraction practically trivial. If you SimpleXML won't do something you need it to, its underpinnings are based on the same library as DOMXML, making swapping between the two a breeze.

2. JSON and SOAP
Web services are all the rage, and blah blah blah. Forget the hype—the reality is that no serious web application can afford to have a single frontend anymore. Your users expect to be able to interface to your code in a multitude of ways and, if you allow them, will come up with new ways you haven't even thought of.

That's why the support for web service functionality built into PHP 5 through the JSON and SOAP interfaces is a boon for developers both because it lets them consume external services—making rich functionality easier to achieve and less expensive—and because it allows them to create interfaces accessible to anyone. Even more importantly, these interfaces make PHP a viable choice for complex backend applications that can be accessed directly from all sorts of Rich Internet development environments like Ajax and Flex.

Bonus functionality: some PHP IDEs (like Zend Studio) are capable of generating WSDL files directly from your PHP source code (thankfully, because writing WSDL files is about as fun as getting a root canal done with a rusty drill). Large vendors, like Adobe and Microsoft, are taking notice of PHP 5's new interoperation capabilities and actually investing in making their products compatible with it and promoting its usage.

3. PDO
I am not entirely sure why, but PDO seems to be one of the great mysteries of PHP 5 (although not as much as SPL—mentioned later in this entry). The idea of accessing multiple databases using a single library is nothing new, of course, but PDO takes an interesting approach to the problem of database abstraction—it only abstracts access to the database, as opposed to the entire interface to it. This means that it doesn't force you to adopt a "lowest-common-denominator" approach in an attempt to make your application function with every possible database, or implement costly abstraction layers. Rather, it allows you to access your database of choice using a simple, consistent interface that promotes good coding practices and provides important functionality like prepared statement and built-in escaping without bogging down your code.

If you are used to DBMS-specific libraries, PDO may seem a little hard to justify until you start using it consistently and find yourself wondering how you managed to live without prepared statements and language-level iterable result sets—to name a couple of the multitude of new features introduced by it.

Bonus features: drivers for all the "popular" DBMS, including MySQL, MSSQL, Oracle and more. Proper, exception-based error reporting.

4. SPL
If I had to pick a favourite-of-favourites in the new features provided by PHP 5, I'd go for the Standard PHP Library. The SPL allows userland code to present itself as one of a number of built-in PHP language elements like arrays. This sounds like little more than a triviality, but the overloading functionality that it provides can be incredibly powerful. The ability to make any object behave like an array—including iteration using foreach()—is perfect for result sets, filtering functionality, and so much more.

SPL is still suffering from a lack of solid documentation, but its functionality is relatively easy to understand, and some articles on its use do exist.

Bonus features: SPL comes with a number of useful built-in classes, such as DirectoryIterator, which makes examining the contents of a directory tolerable, and SimpleXMLIterator, which ties beautifully into SimpleXML. It also implements an enhanced autoloading feature that simplifies dynamic file inclusion considerably. Recursive iterators make for wonderful geek jokes.

5. SQLite
How many times have you wished you could have a cross-platform, portable, powerful, easy-to-use, file-based database system? Perhaps never—but only because you couldn't use SQLite. This simple but full-featured DBMS packs all the punch of MySQL in a format that doesn't require a database server and runs on every platform on which PHP 5 can be compiled. Creating databases is as simple as creating a file, and everything else follows an almost-standard SQL syntax that provides practically everything that you would expect of a modern DBMS—including support for transactions!

SQLite is a perfect choice for data storage where a MySQL database is overkill and a simple text-based file format doesn't provide the performance you need.

Bonus features: SQLite columns are weakly-typed, making it possible to store just about anything. You can also register your PHP functions within the extension and access them from within your SQL statements. SQLite 3 is widely supported by a number of developer technologies, like Flex, making interoperability a breeze. A nice, multiplatform, GUI-based SQLite database editor built using Adobe AIR is readily available.

And much, much more
So, there you have it: five very good reasons to embrace the move to PHP 5. Any one of these five technologies has the potential to revolutionize the way you write code and open up new opportunities for your development efforts.

This is by no means a comprehensive list. Do you have other favourites? Share them below.

23 comments:

Doug Hill said...

SPL autoload methods
Interfaces
__set_state
type hinting
LSB in PHP 5.3

I could go on and on, going back to PHP 4 hurts when I have to do it.

Doug

Anonymous said...

Using PHP native SOAP implementation is a PITA, especially when interoperating with .NET SOAP servers and clients (which use doc/lit wrapped style), because it officially supports only the rpc/encoded style which is almost deprecated, even when PHP5 was released.

Also IIRC many PHP5 stuff is still are marked as "experimental" in the docs, and the documentation is scarce.

Sebastian

Anonymous said...

@Anonymous No problems so far for me. BTW, you can pass "style" and "use" params to the SoapClient config to use doc/lit (see the docs).

Ulf Wendel said...

I can't believe that anybody has been using PDO in production without hassles. PDO/PHP Data Access Abstraction Layer XYZ is a nice idea as such but nobody feels responsible for it since 5.0.

Stefan Priebsch said...

For these and 45 more good reasons to use PHP 5, listen to my talk "50 reasons to use PHP 5" at the upcoming php|tek. So stop posting reasons, Marco, or you will run my talk ;-)

Stefan

Gaylord Aulke said...

For me, Exceptions are by far the most interesting news. Reduces my lines of code my 30%. (dont like type hinting and interfaces so much, though)

Stu said...

Not the list I'd have chosen :)

1. Exceptions
2. Public/protected/private support in objects
3. Object passing by handle
4. Much improved performance
5. Maturing frameworks

Never understood the emphasis on SOAP in the PHP 5 certification, as practically all the systems I've had to integrate with in the past 5 years quickly moved from SOAP to REST (if they ever bothered with SOAP in the first place).

SimpleXML ... handy, but continues PHP's long tradition of buggy XML APIs :( Feels like another piece of abandonware, tbh.

PDO ... also handy, but trying to use prepared statements 100% of the time for improved security hurts performance, because of the limitation of only supporting one prepared statement at a time.

Best regards,
Stu

LornaJane said...

PDO, SOAP and XML handling are all definite selling points for me, as well as real OOP and exceptions. I know SQLite and SPL are great but I haven't had cause to use them in anger yet.

PHP 4 is almost beyond the grave and lots of the problems people had when first upgrading to PHP 5.0 have been eliminated. With 5.3 on the horizon containing the late static binding fix, I can't see any reason to use anything else :)

savaş oyunları said...

Exceptions are by far the most interesting news.

araba oyunları said...

BTW, you can pass "style" and "use" params to the SoapClient config .Thanks

kız oyunları said...

Thanks, for this information and news it was very useful to me

araba oyunları said...

deneme

src belgesi said...

src belgesi src belgeleri

savaş oyunları said...

ı have followed your writing for a long time.really you have given very successful information.
In spite of my english trouale,I am trying to read and understand your writing.
And ı am following frequently.I hope that you will be with us together with much more scharings.
I hope that your success will go on.

kadınca said...

wery good thanks

şifalı bitkiler said...

I can't believe that anybody has been using PDO in production without hassles. PDO/PHP Data Access Abstraction Layer XYZ is a nice idea as such but nobody feels responsible for it since 5.0.

Anonymous said...

徵信, 徵信社, 感情挽回, 婚姻挽回, 挽回婚姻, 挽回感情, 徵信, 徵信社, 徵信, 捉姦, 徵信公司, 通姦, 通姦罪, 抓姦, 抓猴, 捉猴, 捉姦, 監聽, 調查跟蹤, 反跟蹤, 外遇問題, 徵信, 捉姦, 女人徵信, 外遇問題, 女子徵信, 外遇, 徵信公司, 徵信網, 徵信, 徵信社, 外遇蒐證, 抓姦, 抓猴, 捉猴, 調查跟蹤, 反跟蹤, 感情挽回, 挽回感情, 婚姻挽回, 挽回婚姻, 感情挽回, 外遇沖開, 徵信, 徵信, 徵信社, 抓姦, 徵信, 徵信社, 外遇蒐證, 外遇, 通姦, 通姦罪, 贍養費, 徵信, 徵信社, 徵信社, 抓姦, 徵信社, 徵信社, 徵信, 徵信, 徵信公司, 徵信社, 徵信, 徵信公司, 徵信社, 徵信社, 徵信社, 徵信社, 徵信社, 徵信公司, 徵信社, 徵信, 徵信, 徵信公司, 女人徵信, 外遇, 外遇, 外遇, 外遇

徵信, 徵信網, 徵信社, 徵信網, 徵信, 徵信社, 外遇, 徵信, 徵信, 徵信社, 抓姦, 徵信, 徵信社, 外遇, 徵信社, 抓姦, 徵信社, 徵信公司, 徵信, 徵信社, 徵信公司, 徵信, 徵信社, 徵信公司, 徵信社, 徵信社, 徵信社, 徵信社, 徵信, 徵信社, 徵信社, 徵信社, 徵信,

komik oyun said...

I can't believe that anybody has been using PDO in production without hassles. PDO/PHP Data Access Abstraction Layer XYZ is a nice idea as such but nobody feels responsible for it since 5.0.

kız giydirme oyunları said...

For me, Exceptions are by far the most interesting news. Reduces my lines of code my 30%. (dont like type hinting and interfaces so much, though)

Anonymous said...

~「朵語‧,最一件事,就。好,你西中瀟灑獨行。

夏天 said...

搬家搬家搬家公司 搬家搬家Shade sailnike shoesMBA在职研究生 在职博士徵信社 徵信室內設計室內設計代償房屋貸款信用貸款外遇離婚抓姦外遇蒐證外遇抓姦侵權仿冒應收帳款工商徵信徵信 徵信社外遇徵信徵信社外遇电动隔膜泵自吸泵化工泵离心泵磁力泵螺杆泵水泵隔膜泵气动隔膜泵百家乐 轮盘 21点 德州扑克 百家乐系统 真人娱乐场 百家乐足球德州扑克 电子游戏 英格兰超级联赛 德国甲组联赛 意大利甲组联赛西班牙甲组联赛法国甲组联赛欧冠杯 英超 足球比分 足球彩票 体育彩票 即时比分 免費a片 a片 免費av 色情影片 情色 情色網 色情網站 色情 成人網成人圖片成人影片 18成人 av av女優 avav女優 情慾 走光 做愛 sex H漫 情色 情趣用品 情色 a片 a片 成人網站 成人影片 情趣用品 情趣用品アダルトアダルト アダルトサイト アダルトサイト 情趣用品搬家搬家服務搬家保障搬家網搬家估價徵信徵信的意義徵信服務徵信報導徵信問答徵信知識婚禮佈置 婚禮佈置

bobo said...

This is the latest and hottest ghd styler ever. If you need a ghd hair straighteners, this is a must buyghd hair straighteners,cheap ghd hair straighteners,pink ghd hair straightenersghd straightenersComme vous pouvez le voir, il s'agit d'une paire de chaussures shox classiquepink ghd hair straighteners . Si vous souhaitez poursuivre la mode, nike shox NZ sont votre meilleu…"Cette paire de Nike Shox Torch est chaud en maintenant la demande.nike chaussurestn chaussures

Anonymous said...

I can get Tibia Gold cheaply.
Yesterday i bought Tibia Platinum for my brother.
i hope him like it. i will give Tibia coins to him
as birthday present. i like the Tibia money very much.
I usually buy tibia gp and keep it in my store.