Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!


Forgot your password?

Going Dynamic with PHP 222

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."
This discussion has been archived. No new comments can be posted.

Going Dynamic with PHP

Comments Filter:
  • Experiences (Score:5, Interesting)

    by truthsearch (249536) on Monday February 20, 2006 @02:19PM (#14762343) Homepage Journal
    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.
  • Interesting, but ... (Score:3, Interesting)

    by isolationism (782170) on Monday February 20, 2006 @02:54PM (#14762566) Homepage
    ... There hasn't exactly been widespread adoption of PHP5 at commercial hosting interests, has there. I see PHP's greatest strength being its widespread adoption, not its OO model -- and it doesn't do a lot of good to develop using a technology that is effectively unavailable to host your application unless you want to set up your own co-located server to do it (e.g. even top-notch managed services like Rackspace still come with PHP4).

    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.

  • Re:OOP (Score:3, Interesting)

    by yintercept (517362) on Monday February 20, 2006 @04:03PM (#14762985) Homepage Journal
    OOP is transcendant. OO programmers are on a higher level of existence than procedural programmers. When I OOP, I am charged revolutionary spirit. It feels great! I love thinking in objects and discussing patterns while smoking a pipe in an easy chair.

    Sadly, the main use for PHP is to jam out a script to get a job done. Much as I love objects. I can't help but notice that my primary use of PHP is to grab a string of data from MySQL, pack some HTML around it and present the string to a web server. Writing code to translate a string from a database into HTML is not substantially enhanced by OOP.

    When I want a full object model ... I really don't want to use a scripting language.

    As much as I love the revolutionary spirit of OOP, I worry that maybe there's a part of me that is still just petty bourgeoisie. Even with PHP 5, I find that my PHP code comes out in the form of procedures. The game of wrapping a string from a database in HTML code is easier with procedures.

    It makes me feel shamed, but, I still keep writing procedures.
  • Re:PHP 4 V. 5 (Score:3, Interesting)

    by jZnat (793348) * on Monday February 20, 2006 @04:25PM (#14763107) Homepage Journal
    PHP5 uses MySQLi [php.net] 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 [php.net] for a unified database interface (although it is a bit primitive when compared to PEAR DB [php.net] and other DBIs).
  • by PietjeJantje (917584) on Monday February 20, 2006 @04:31PM (#14763137)
    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.
  • Re:PHP (Score:3, Interesting)

    by DavidTC (10147) <slas45dxsvadiv...vadiv@@@neverbox...com> on Monday February 20, 2006 @06:11PM (#14763638) Homepage
    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.

    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.

  • Awful use of regex (Score:3, Interesting)

    by 1110110001 (569602) <slashdot-0904@ne[ ]at ['dt.' in gap]> on Monday February 20, 2006 @08:48PM (#14764537)
    The call method has a preg_match, which is not needed and doesn't match right:
    function __call($method, $args) {
      if(preg_match("/set_(.*)/", $method, $found )) {
        if(array_key_exists($found[1], $this->fields)) {
          $this->fields[$found[1]] = $args[0];
          return true;
      } else if(preg_match("/get_(.*)/", $method, $found)) {
        if(array_key_exists($found[1], $this->fields)) {
          return $this->fields[ $found[1] ];
      return false;
    There is so too much wrong with this simple method:
    • Double quotes altough single qoutes would do it
    • No circumflex at start of regex, thus somerandomshitset_foobar also matches
    • Code is duplicated (array_key_exists)
    • Null would fit much better if someone is trying to use a random name as getter, false could be a valid value for a field
    • $args is used but not checked
    • A string is splitted with an regex?!

    Let's see how it can be done better:
    function __call($method, $args) {
      list($type, $field) = explode('_', $method, 2);
      if(!isset($this->fields[$field])) {
        return null;
      if($type == 'get') {
        return $this->fields[$field];
      if($type == 'set' && isset($args[0])) {
        $this->fields[$field] = $args[0];
        return true;
      return null;

The disks are getting full; purge a file today.