Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Going Dynamic with PHP

Posted by ScuttleMonkey on Mon Feb 20, 2006 01:06 PM
from the not-the-bastard-child-of-languages-anymore dept.
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.
Display Options Threshold:
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • PHP's Comeupance (Score:5, Informative)

    I rediscovered PHP last week after a year-long hiatus, and was surpised to find that it approacheth Java in reflection and information hiding; that said, there are some lamentable lacunæ:
    • 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 [doxygen.org].
    • Re:PHP's Comeupance (Score:5, Funny)

      by Hal_Porter (817932) on Monday February 20 2006, @02:45PM (#14762885)
      Dear (1+-sqrt(5))*(2**-1),

      I must say, Sir, that I salute you for your post. Not only is in Informative, it is also well drafted in the Queens English, not in the debased language of Internetland. Furthermore, you have managed to obtain the coveted pole position of posters, so to speak. As I write this epistle of congratulations, I shall drink a toast of finest port wine to you, and I wish that your correspondence with the world shall continue for all eternity.

      If I may be so bold as to trouble you with two requests, it would be these. Firstly, may I post a copy of your memorandum above the post to which I fasten my servants when I whip them for dumb insolence and/or sundry grammatical errors, including the splitting of infinitives or the use of the accursed Internet language? Secondly, my wife is expecting our first child, conceived soon after she spent a night in my servants quarters, teaching them virtue of prayer. Would it be presumptive of me to command her to name her baby (1+-sqrt(5))*(2**-1). We are unsure at present of the sex of the baby, since we have emigrated to the 19th Century to escape the linguistic slovenliness of the later epochs, but we feel that such a name would distinguish a child of either gender?

      Yours, etc
      Hal Porter, esq.
      [ Parent ]
    • Re:PHP's Comeupance (Score:4, Informative)

      by sweetnjguy29 (880256) on Monday February 20 2006, @04:39PM (#14763465)
      (Last Journal: Friday March 24 2006, @12:46PM)
      Learned a new word: lacunæ (latin for lake) -- in this context, a gap. Well done, sire!
      [ Parent ]
    • Re:PHP's Comeupance by sasdrtx (Score:1) Monday February 20 2006, @04:59PM
    • Re:PHP's Comeupance by stonecypher (Score:2) Tuesday February 21 2006, @12:06PM
    • Re:PHP's Comeupance by (1+-sqrt(5))*(2**-1) (Score:1) Monday February 20 2006, @01:36PM
    • 1 reply beneath your current threshold.
  • PHP 4 V. 5 (Score:1)

    by wesw02 (846056) on Monday February 20 2006, @01:10PM (#14762277)
    I'm curious about the Differences between PHP 4 and PHP 5, can someone provide a link, or first hand info about the good and bad of PHP 5.
    • Re:PHP 4 V. 5 by karvind (Score:1) Monday February 20 2006, @01:16PM
    • Re:PHP 4 V. 5 by drpimp (Score:3) Monday February 20 2006, @01:19PM
    • Re:PHP 4 V. 5 (Score:5, Informative)

      by lbft (950835) on Monday February 20 2006, @01:23PM (#14762374)
      (http://shock-horror.com/)

      There's a good summary on Zend: http://www.zend.com/php5/andi-book-excerpt.php [zend.com]

      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 [oreilly.com] if you're familiar with PHP 4.

      [ Parent ]
      • Re:PHP 4 V. 5 by Heembo (Score:1) Monday February 20 2006, @02:43PM
        • Re:PHP 4 V. 5 by Parham (Score:2) Monday February 20 2006, @03:05PM
        • Re:PHP 4 V. 5 by jZnat (Score:3) Monday February 20 2006, @03:22PM
        • Re:PHP 4 V. 5 (Score:5, Insightful)

          by masklinn (823351) <{slashdot.org} {at} {masklinn.net}> on Monday February 20 2006, @04:40PM (#14763467)

          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.

          [ Parent ]
          • Re:PHP 4 V. 5 by tinkertim (Score:1) Tuesday February 21 2006, @04:19AM
            • Re:PHP 4 V. 5 by masklinn (Score:2) Tuesday February 21 2006, @05:58AM
        • Re:PHP 4 V. 5 by jo42 (Score:1) Monday February 20 2006, @05:08PM
      • Re:PHP 4 V. 5 by kpdvx (Score:1) Monday February 20 2006, @06:28PM
        • Re:PHP 4 V. 5 by lbft (Score:2) Monday February 20 2006, @07:18PM
          • Re:PHP 4 V. 5 by hazah (Score:1) Tuesday February 21 2006, @11:52AM
      • PDO in v5.1 by PhYrE2k2 (Score:2) Tuesday February 21 2006, @09:57AM
      • 1 reply beneath your current threshold.
    • Re:PHP 4 V. 5 (Score:5, Informative)

      by Foofoobar (318279) on Monday February 20 2006, @01:35PM (#14762447)
      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.
      [ Parent ]
      • Re:PHP 4 V. 5 by Anonymous Coward (Score:2) Monday February 20 2006, @02:16PM
      • Re:PHP 4 V. 5 by jZnat (Score:3) Monday February 20 2006, @03:25PM
        • Re:PHP 4 V. 5 by jZnat (Score:2) Monday February 20 2006, @05:16PM
      • 1 reply beneath your current threshold.
    • Re:PHP 4 V. 5 by HaydnH (Score:2) Tuesday February 21 2006, @04:38AM
    • 1 reply beneath your current threshold.
  • by xxxJonBoyxxx (565205) on Monday February 20 2006, @01:17PM (#14762329)
    If you're going to be this generic, why lock each class to a single table? (Why not let the name of the table be an argument as well?)

    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.)

  • Experiences (Score:5, Interesting)

    by truthsearch (249536) on Monday February 20 2006, @01:19PM (#14762343)
    (http://seenonslash.com/ | Last Journal: Friday May 11 2007, @04:02PM)
    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.
    • Re:Experiences by JabberWokky (Score:2) Monday February 20 2006, @01:46PM
      • Re:Experiences by T-Ranger (Score:2) Monday February 20 2006, @02:05PM
      • Re:Experiences by shmlco (Score:3) Monday February 20 2006, @04:41PM
        • Re:Experiences by Mr. Slippery (Score:1) Tuesday February 21 2006, @12:30PM
    • Re:Experiences by killjoe (Score:2) Monday February 20 2006, @03:07PM
    • Re:Experiences by 1110110001 (Score:2) Monday February 20 2006, @07:22PM
    • Re:Experiences (Score:4, Insightful)

      by Tenareth (17013) on Monday February 20 2006, @02:48PM (#14762906)
      (http://www.lawyers.com/)
      Yeah, the inability to scale is the primary reason all the porn sites use it...

      And why we have a lot of high traffic sites that have no issue with it...

      Threading != Scalability. Threading is only needed if you do certain types of tasks that need it.

      In programming, there is no silver bullet, there are lots of tools suited for different jobs, PHP fits in a lot of those places, Java in others, other languages elsewhere. Threading isn't a "Oh, I'll multithread it, that'll make it faster" type of magic button.

      [ Parent ]
    • Re:Experiences by 1110110001 (Score:2) Monday February 20 2006, @07:27PM
    • 1 reply beneath your current threshold.
  • OOP (Score:5, Insightful)

    by onion2k (203094) on Monday February 20 2006, @01:29PM (#14762409)
    (http://www.phpgd.com/)
    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.
    • Re:OOP by truthsearch (Score:2) Monday February 20 2006, @01:39PM
    • Re:OOP by Tablizer (Score:2) Monday February 20 2006, @01:58PM
      • Re:OOP (Score:5, Insightful)

        by IgnoramusMaximus (692000) on Monday February 20 2006, @02:34PM (#14762828)
        I don't want to start an OOP war, but am just suggesting that people not use objects just because somebody says to (unless they are your boss and order you).

        I am with you on this one, although, unlike you, I used to be swept up at one time in all the early OO hype to a degree (back in the Smalltalk days - which by the way is probably the only coherent OO environment), only to realise, by experience, that it was for the most part just that: unsubstantiated hype. Object Orientation has applications, notably in situations close to its original aims of simulation of physical universe (as in the Simula language which started it all), which in modern software is found in things like PC games. But this paradigm has been overused, abused and stretched beyond its breaking point to try to cover all possible cases, no matter how ill fitting. And in the process it has consistently failed to deliver on most of its promises (other then to pad pockets of various charlatans and OO "framework, IDE, grill and taco stand" makers). My experience has thought me that contrary to what OOP priests wish us to believe, "complexity hiding" (usually by masking it by more OO-gunk complexity) is not the answer to reusability, cooperative maintenance and so on. The answer is: simplicity. That is the most maintainable code is the one which can be fully understood, with ease, by the programmer. OOP does not do that. In fact it introduces a whole new gamut of problems dealing with the hidden workings of pyramidal heaps of classes found in any OOP "framework", each with its own weird quirks and inconsistencies, from which you are supposed to "derrive" your own, as long as you adhere to a million of unwritten and poorly documented rules of use of these, supposedly, "reusable" and "extensible" components. Not to mention that it actively encourages creation of massive, incomprehensible class jungles as one can easily see in places like the JDK.

        Speaking of PHP, its success can be attributed to its simplicity, ease of use and short learning curve. If they keep continuing to "innovate" new piles of ever more trendy crap into the language, it will soon (already is?) lose this advantage. It won't be long before someone comes up with another simple server-side language to replace the morass and the enthusiatic crowds will move onto this new "lighning fast, low footprint" language and begin to "improve it". Rinse, repeat.

        [ Parent ]
        • Re:OOP by Mateo_LeFou (Score:1) Monday February 20 2006, @04:24PM
        • Re:OOP by stedo (Score:1) Monday February 20 2006, @04:47PM
        • Re:OOP by DavidTC (Score:1) Monday February 20 2006, @05:20PM
        • Re:OOP by TLLOTS (Score:2) Monday February 20 2006, @05:43PM
          • Re:OOP by IgnoramusMaximus (Score:2) Monday February 20 2006, @06:05PM
            • Re:OOP by TLLOTS (Score:2) Monday February 20 2006, @06:18PM
              • Re:OOP by IgnoramusMaximus (Score:2) Monday February 20 2006, @06:40PM
        • Re:OOP by Dracolytch (Score:2) Friday March 03 2006, @10:14AM
      • Re:OOP by ctr2sprt (Score:3) Monday February 20 2006, @03:01PM
        • Re:OOP (Score:5, Insightful)

          by IgnoramusMaximus (692000) on Monday February 20 2006, @03:23PM (#14763097)
          Let me take this silly argument to its full comical potential:

          If you're a programmer, I guarantee that you're using SEP (Spiritual Extrusion Programming) even if you don't use a SEP language.

          Consider the Unix read() function, which can read from any file descriptor: regular file, block or character special, socket, and so on. If you were to write such a function in C, you'd write some code to determine the underlying type of descriptor. Then, based on that type, you'd call a helper function - read_file(), read_special(), read_socket() - to perform the actual read. Guess what? That's SEP. The file descriptor is the Spititual Manifestation of a Concept. The code at the top of read() is determining the object type (or Spiritual Element). And the helper functions are Spiritual Invocations. If this is a big project, odds are you'll put all the helper functions for sockets in socket.c, for files in file.c, and so on. And there we have Spiritual Encapsulation (of the namespace variety) too.

          As noted many times, you can write an SEP in any language. It's just easier to do it in a language that's designed for it.

          And so on, etc.

          Newsflash: all of the concepts you describe are 1) long in existence before your favourite paradigm arrived on the scene so that you can try to contort them into it, and 2) can be interpreted ad infinitum with any of the millions of "paradigms" one can concoct on the spot. That is because your paradigm is merely a perspective and not an universal (and only) truth. The whole point of abstract formulations, such as computer software, is that they can conform to nearly infinite number of perspectives and can be projected onto the real, physical universe in an equally large number of ways, which is what makes this whole Information Technology thing so powerful.

          But I fear that long after OOP has been relegated to the dusty bin of history, some ctr2sprt of the future will still argue about how you are really always writing "Quantum Parallax" code, even if you are you are not ...

          [ Parent ]
          • Re:OOP by Decaff (Score:2) Tuesday February 21 2006, @07:05AM
            • Re:OOP by IgnoramusMaximus (Score:2) Tuesday February 21 2006, @07:56PM
              • Re:OOP by Decaff (Score:2) Wednesday February 22 2006, @08:35AM
              • Re:OOP by IgnoramusMaximus (Score:2) Wednesday February 22 2006, @11:30AM
              • Re:OOP by Decaff (Score:2) Wednesday February 22 2006, @03:33PM
              • Re:OOP by IgnoramusMaximus (Score:2) Wednesday February 22 2006, @06:22PM
              • Re:OOP by Tablizer (Score:1) Wednesday February 22 2006, @10:27PM
              • Re:OOP by Decaff (Score:2) Thursday February 23 2006, @06:23AM
              • Re:OOP by Tablizer (Score:1) Thursday February 23 2006, @08:46PM
          • Re:OOP by StormReaver (Score:2) Tuesday February 21 2006, @10:49AM
            • Re:OOP by IgnoramusMaximus (Score:2) Tuesday February 21 2006, @08:06PM
            • Re:OOP by IgnoramusMaximus (Score:2) Tuesday February 21 2006, @08:21PM
            • Re:OOP by Tablizer (Score:1) Wednesday February 22 2006, @10:22PM
        • Re:OOP by Tablizer (Score:1) Monday February 20 2006, @03:32PM
      • Re:OOP by yintercept (Score:3) Monday February 20 2006, @03:03PM
        • Re:OOP by Tablizer (Score:1) Monday February 20 2006, @03:43PM
          • Re:OOP by yintercept (Score:2) Monday February 20 2006, @06:25PM
            • Re:OOP by Tablizer (Score:1) Monday February 20 2006, @10:17PM
        • Re:OOP by Foofoobar (Score:2) Tuesday February 21 2006, @10:39AM
          • Re:OOP by yintercept (Score:2) Tuesday February 21 2006, @01:41PM
            • Re:OOP by Foofoobar (Score:2) Wednesday February 22 2006, @12:40AM
              • Re:OOP by Tablizer (Score:1) Saturday February 25 2006, @03:29PM
      • 1 reply beneath your current threshold.
    • Re:OOP by Fahrenheit 450 (Score:2) Monday February 20 2006, @02:16PM
    • Re:OOP by Slashcrunch (Score:1) Monday February 20 2006, @08:56PM
  • Hosting services stuck on PHP4 (Score:4, Informative)

    by cknudsen (891397) on Monday February 20 2006, @01:32PM (#14762424)
    (http://www.devpointer.net/)
    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.
    FYI.... PHP developer articles updated daily:
    http://www.devpointer.net/browse.php?l=p&t1=1 [devpointer.net]
    RSS:
    http://www.devpointer.net/browse.php?l=p&t1=1&fmt= rss1 [devpointer.net]
  • by tres (151637) on Monday February 20 2006, @01:33PM (#14762432)
    (http://blas.phemo.us/)
    From the article:


    Dynamic objects have a lot of power, but they also carry significant risk. First, the complexity of classes increases tremendously when you start writing magic methods. These classes are harder to understand, debug, and maintain. In addition, as integrated development environments (IDEs) become more intelligent, they may experience problems with dynamic classes such as these because when they look up methods on a class, they won't find them.


    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 [bell-labs.com]) 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.

  • by aaronvegh (546815) on Monday February 20 2006, @01:34PM (#14762438)
    Cool article, but the first paragraph where he's deciding to write a single class to handle any database table, while cool, isn't as cool as PHP Object Generator, the tool I use to handle my databases. Ch-ch-check it out: Right here [phpobjectgenerator.com]. It's sweet.

    Okay, back to nerding.

  • Kinda Like Ruby (Score:3, Insightful)

    by pkulak (815640) on Monday February 20 2006, @01:35PM (#14762448)
    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.
    • Ruby? by Some Random Username (Score:3) Monday February 20 2006, @04:50PM
      • Re:Ruby? by pkulak (Score:1) Monday February 20 2006, @04:55PM
        • Re:Ruby? by Dr. Sp0ng (Score:2) Monday February 20 2006, @05:21PM
        • Re:Ruby? by Some Random Username (Score:2) Monday February 20 2006, @08:01PM
  • by Yuioup (452151) on Monday February 20 2006, @01:36PM (#14762452)
    "welcome to 1967".

    http://en.wikipedia.org/wiki/Simula [wikipedia.org]

    Y
  • by Tablizer (95088) on Monday February 20 2006, @01:42PM (#14762485)
    (http://www.geocities.com/tablizer | Last Journal: Saturday March 15 2003, @01:22PM)
    Here are two other approaches to consider:

    1. Use maps (associative arrays) instead of objects. If it is mostly just attributes, then the difference will be small. Map syntax is usually more compact than object syntax, keeping the code clean.

    2. Create a data dictionary table where field attributes and even code or function calls (used with eval() function) are kept. However, you will need to find a good table browser to enter all that conveniently. A data dict table may not always be the most efficient from a machine standpoint, but can make for good RAD if used well.
         
  • Crap... (Score:5, Funny)

    by A beautiful mind (821714) on Monday February 20 2006, @01:46PM (#14762509)
    I've clicked to another tab to browse at some other site and then I've seen suddently:

    "Slashdot | Going Dynamic with PHP"

    ...as the title of the Slashdot tab. It gave me the creeps until I remembered whats the article about. Phew.
    • Re:Crap... by RancidMilk (Score:1) Tuesday February 21 2006, @07:55AM
  • Interesting, but ... (Score:3, Interesting)

    by isolationism (782170) on Monday February 20 2006, @01:54PM (#14762566)
    (http://www.isolationism.com/)
    ... 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.

  • by guice (907163) on Monday February 20 2006, @01:58PM (#14762609)
    I have to question the writer's competence in PHP. For one, take:
    $book->{'title'} = "PHP Hacks"; $book->{'author'} = "Jack Herrington"; $book->{'publisher'} = "O'Reilly";
    pretty Perl esque. That even work in PHP? I never would have figured. I've personally kept my Perl etiquettes very sepereate from PHP etiquettes. Then you have his DB assignment:
    $db =& DB::Connect( $dsn, array() );
    In PHP5 all objects are passed by reference, making =& an E_STRICT warning.

    Linking some form of programming and variable naming convention would have been nice, too. He doesn't have to go into detail, just lay mention to it. Something like "Just fyi, I follow X conventions when naming my classes, variables, etc..."

    Then you tack on the complete lack of comments! ^_^

  • DB_DataObject (Score:2, Informative)

    by schmidt (69858) on Monday February 20 2006, @02:19PM (#14762735)
    (http://aggemam.dk/)
    There is an implementation of this idea (and more) in PEAR's DB_DataObject package:

    http://pear.php.net/manual/en/package.database.db- dataobject.php [php.net]
    • 1 reply beneath your current threshold.
  • by zardo (829127) on Monday February 20 2006, @02:25PM (#14762774)
    I was following PHP5 development until the RC1 deployment, and I was disappointed. There wasn't really much, I yearned for a new, truly OO language. Luckily, about the same time, Ruby on Rails was getting a lot of attention and I checked that out, now I couldn't be happier. I do some contract work in PHP4 and PHP5 sometimes, and I just get this sick feeling. Honestly I'd much rather program in Perl than PHP.
  • by Salamanders (323277) on Monday February 20 2006, @02:36PM (#14762838)
    Combining the dynamic getters and setters with the PEAR DB_Table (http://wiki.ciaweb.net/yawiki/index.php?area=DB_T able [ciaweb.net]) class would truly kick ass - full run-time definition of tables, HTML_QuickForms, and all that other whiz-bang stuff. No more .sql scripts anywhere, hell, no more SQL anywhere!

    Can RoR do that? (Honest question, I'm a PHP guy, but have heard a lot about RoR)

    DB_Table is worth checking out.
  • The one thing I was disappointed about this article is that it doesn't make a "describe $table" call to get the fields for you. You could then extend DBObject to like DBO_customer and the constructor would yank "customer" out of the class name. So then all you really have to do is extend and rename and that's it. No need to directly pass the table name to the constructor.

    Secondly, by modifying the load function to accept an array of id's and implementing the Iterator interface, one can use the object in a foreach loop to iterate over each customer (perhaps by implementing DBObject::current() to instantiating a new object and populating it with data for a specific customer thus maintaining objects within the foreach loop). (The trick is to tie the original object into the new one so a data update in the new object is reflected back to the original.) Maybe even add in transaction support on update & delete calls.

    Thirdly, if you add in "dirty" and "new" flags to each row stored in the object then you can combine insert, update, & delete calls in a single DBObject::sync() call (again wrapped in a transaction if you wish).

    Fourthly, don't forget __unset() and __isset() to remove or check for fields.

    Fifthly, store the other columns from the describe call and do validation and/or type conversion automatically.

    Finally, __sleep() and __wakeup() would be useful for serializing objects to a session or something. That one's easy though: just serialize() your fields array and unserialize on wakeup.

    One more difficult step I have yet to try is supporting joins (perhaps you want to load an object with a customer and all of their orders) which would save the need to use 2+ objects.
  • by TLLOTS (827806) on Monday February 20 2006, @02:55PM (#14762945)

    While the approach in the article does appear to be quite good for rapid code generation, I personally wouldn't touch it with a ten foot pole if I had a choice, though I was unfortunate enough to have to work with such a setup recently.

    The problem with this kind of setup is that while it works well for simple work, the moment you try and do something more complex, you'll find you have to fight against the API, and the code will end up being a mess in order to do a task that should be quite simple.

    Naturally I have a somewhat different approach that's somewhat weird, but I've enjoyed great success when using it. I have one database object called 'database' funnily enough ;) Inside this database object there are four other objects, select, insert, update & delete. Now, each of these four objects aren't anything special, they're simply wrappers around methods of a particular type (select queries go into select for example).

    To give an example, where the author in the example wrote

    $book->{'title'} = "PHP Hacks";
    $book->{'author'} = "Jack Herrington";
    $book->{'publisher'} = "O'Reilly";
    $id = $book->insert();

    I would simply write

    $book_result = $database->insert->book("Jack Herrington","O'Reilly");

    This method will return a result object, from which I can retrieve the id simply by typing

    $book_result->fetch_insert_id();

    Now how does the internals of this work? I won't say, simply because it doesn't actually matter, which is the whole point of this kind of setup. It provides a very simple API, so that in the back I can make it work however I want, allowing me to easily switch in different databases, or if I wanted to I could make it work with flat text files. This way I keep all my messy SQL contained in one area and I don't see it anywhere else, making it much easier and cleaner than the method offered in the article. However, my method will require more work to setup initially, and naturally it won't automatically adapt to changing database tables... though I have to wonder about the quality of a project wherein the tables are changing frequently, that's the kind of thing one should have planned out from the beginning.

    That said, I'm sure there is some value in setups like the one mentioned in the article, and my own database abstraction code is hardly perfect as I'm still something of a beginner at programming in many ways.

  • OO and PHP? What's the fuss.. (Score:2, Interesting)

    by PietjeJantje (917584) on Monday February 20 2006, @03: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.
  • by skogs (628589) on Monday February 20 2006, @04:01PM (#14763271)
    (Last Journal: Friday June 30 2006, @11:10PM)
    that actually thought it was a well done article?

    Seems a bit better than some of the journalist's moronic rantings that I've seen lately on /.

    Good food for thought, good examples, good examples of the new functions that replace some of the fubar functions in 3 & 4.

  • love php5 (Score:1)

    by wwmedia (950346) on Monday February 20 2006, @04:08PM (#14763304)
    (http://www.footballfans.tv/)
    i love php5

    recently upgraded my servers to php5.1.2 and mysql5 running on apache2

    all my sites are programmed in OO php5 (i know it aint as good as java, but it does the job)

    the speed increase has been phenomenal (i think mainly to php5 using references instead of passing by value)

    i have a forum with 200 people on at any time with the server load barely above 1.0!! thats a celeron server as well!
    • 1 reply beneath your current threshold.
  • Awful use of regex (Score:3, Interesting)

    by 1110110001 (569602) <alpha&rrs,at> on Monday February 20 2006, @07:48PM (#14764537)
    (http://alpha.edtinger.at/)
    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;
    }
  • by CodeoRowboy (940385) on Monday February 20 2006, @07:51PM (#14764557)
    you would live to see IBM become "hip" again in terms of technology? I can remember when they were the MS of the computer world - evil empire, monolithic - all that good stuff. Now they are like the mythic figure of Richard the Lionheart - ready to ride in and assist the open source Robin Hood.

    CodeoRowboy
    "The speed of light is faster than the speed of sound. That is why some people appear bright until you hear them speak."
  • by cwf0621 (948557) on Monday February 20 2006, @08:01PM (#14764602)
    My firm is a fairly big fan of WASP (http://wasp.sourceforge.net/ [sourceforge.net]). Check it out.
  • by spentrent (714542) on Monday February 20 2006, @11:23PM (#14765390)

    The third solution, which I cover in this article, is to write a single class that dynamically molds itself at runtime to the fields of a given table. This class may perform a bit more slowly than its table-specific counterpart, but it saves me from having to write a lot of code. This solution is particularly beneficial at the start of a project, where tables and fields are changing constantly, and keeping up with rapid changes is crucial.

    OK bro so I am a kitschy Perl programmer who loves invoking "Laziness" but this is TOOOOO much.

    Now you can write code that is twice as shitty in half the time! And that's a feature?!?

  • Re:PHP (Score:1)

    by drpimp (900837) on Monday February 20 2006, @01:22PM (#14762365)
    (Last Journal: Tuesday September 11, @06:14PM)
    Or people that can't wait for Perl 6 to come out.
    Or people that don't want to use Python for web interfaces.

    IMHO, Perl is too cryptic
    [ Parent ]
  • by B3ryllium (571199) on Monday February 20 2006, @01:26PM (#14762393)
    (http://www.beryllium.ca/)
    If you think those look similar, I can't begin to imagine your opinion of every company's logo for the last six years.
    [ Parent ]
  • by xxxJonBoyxxx (565205) on Monday February 20 2006, @01:34PM (#14762436)
    Infighting between PHP and Perl? Really.

    Once you get to know a couple of other programming languages, PHP and Perl look quite similar indeed. (Kind of like VBS and ASP, Java and C#, C and C++, etc. are similar.)

    [ Parent ]
  • Re:PHP (Score:5, Funny)

    by Ford Prefect (8777) on Monday February 20 2006, @01:39PM (#14762461)
    (http://www.hylobatidae.org/minerva/)
    PHP: The Language For People Who Can't Code Perl

    Oh, but everyone (and everything) can code perl - the trick is to hit the keys in a sufficiently random manner.
    A$u^UD&KRWTuKUI^R&54$%A%ERYTi:",<%$E^%L<E%@$U%^*E
    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... ;-)
    [ Parent ]
    • Re:PHP (Score:5, Funny)

      by deep44 (891922) on Monday February 20 2006, @01:48PM (#14762524)
      Can anybody else read this comment? My browser keeps crashing..
      [ Parent ]
    • Re:PHP (Score:4, Funny)

      by nbritton (823086) on Monday February 20 2006, @02:29PM (#14762796)
      @P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
      @p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q *=2)+=$f=!fork;map{$P=$P[$f^ord
      ($p{$_})&6];$p{$_ }=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[ P.]/&&
      close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
      [ Parent ]
      • 1 reply beneath your current threshold.
  • Re:PHP (Score:1, Flamebait)

    by A beautiful mind (821714) on Monday February 20 2006, @01:48PM (#14762525)
    How true.

    People who don't know Perl are doomed to reinvent it. Badly. ;)
    [ Parent ]
    • Re:PHP by iBod (Score:2) Monday February 20 2006, @02:04PM
    • Re:PHP by larry bagina (Score:1) Monday February 20 2006, @05:03PM
  • Any fool can code Perl... (Score:5, Insightful)

    by iBod (534920) on Monday February 20 2006, @01:48PM (#14762529)
    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).
    [ Parent ]
  • Re:PHP (Score:4, Funny)

    by Unski (821437) on Monday February 20 2006, @01:48PM (#14762531)
    (Last Journal: Sunday May 21 2006, @05:16PM)
    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 *
    [ Parent ]
    • Re:PHP by eargang (Score:1) Monday February 20 2006, @01:56PM
    • Re:PHP by WilliamSChips (Score:1) Monday February 20 2006, @02:30PM
      • Re:PHP by Unski (Score:1) Monday February 20 2006, @02:49PM
    • Re:PHP by woolio (Score:1) Monday February 20 2006, @03:07PM
    • Re:PHP by DavidTC (Score:3) Monday February 20 2006, @05:11PM
      • 1 reply beneath your current threshold.
    • Re:PHP by CTachyon (Score:2) Saturday February 25 2006, @01:04AM
  • PHP will eventually get all that J2EE goodness. Take a look at the trunk (PHP 6) for examples.
    [ Parent ]
  • Re:LOL (Lisp) (Score:1)

    by Tablizer (95088) on Monday February 20 2006, @04:03PM (#14763281)
    (http://www.geocities.com/tablizer | Last Journal: Saturday March 15 2003, @01:22PM)
    LISP, bitches!

    I'll give you some advice. If you love Lisp so much, find a better way of selling it. Lisper's have done a horrible job of convincing others why it is so great.

    May I suggest writing a publicly viewable sample application that is fairly practical, not lab toys or programming contest entries; and then compare it to implimentations in non-Lisp languages and describe in detail why it is better.

    For example, a college grade and course tracking system.
             
    [ Parent ]
    • 1 reply beneath your current threshold.
  • Re:PHP (Score:2)

    by masklinn (823351) <{slashdot.org} {at} {masklinn.net}> on Monday February 20 2006, @04:51PM (#14763525)
    Really, no.
    [ Parent ]
  • And types don't even flee the field at runtime (as Java's types do), they're not here to being with.
    [ Parent ]
  • 11 replies beneath your current threshold.