75 Comments
- MikeSD34, on 10/12/2007, -3/+23Is it still a lost art if it was never found to begin with?
- inactive, on 10/12/2007, -5/+15Its a lost art because the programming abilities of the average php developer aren't as mature as the average Java programmer. It is rare to find object oriented implementations in php because the community on average doesn't know OOP practices.
Before you get all bent out of shape i said the "average developer" not all. - bieber, on 10/12/2007, -1/+10"the programming abilities of the average php developer aren't as mature as the average Java programmer."
Gotta call ***** on that one. For every inexperienced PHP programmer who just wants to be able to throw crap together, there's an equally inexperienced Java programmer who only uses it because he used it in CS or some such thing, and only uses object orientation because the language forces him to. Morover, use of OO techniques does not indicate programming skill, it indicates personal preference. - mcspectrum, on 10/12/2007, -0/+8PHP is a multiple-paradigm language and it thrives because of that, not in spite of that. I agree with thread & ahawks on this. I don't use objects all the time because they are slower, and often unnecessary. I run across so much code where the programmers just used objects for the sake of using objects, because they heard it was the right thing to do, instead of actually understanding when and why to use objects. It drives me almost as crazy as spaghetti code does.
- thread, on 10/12/2007, -2/+10I call bs! Many PHP frameworks and projects make extensive use of PHP's OO capabilities -- especially with all the "real OO" stuff in PHP5. (ruby/rails is where it's at, though... 100% OO, baby.)
- saifatlast, on 10/12/2007, -0/+7What do you mean you have to use $this every time? Most other OO languages have a similar construct.
- SamKellett, on 10/12/2007, -0/+7I agree about Ruby. It's just that PHP is so much easier to learn as a first programming language that you become trapped and, although I like PHP a lot, I admit it teaches some ideas of programming differently, which do not help when you eventually move to a more OO language (like Java or Ruby or Python).
PHP is the most success because of it's documentation. Period. - inactive, on 10/12/2007, -0/+6Perhaps the word you are looking for is "persistence"?
...and there are many ways to maintain object persistence in PHP. - Stopher, on 10/12/2007, -1/+6Beat me to it.=)
- marnaq, on 10/12/2007, -0/+5They've done a great job with PHP5.
I'm hoping for namespaces in PHP6. - craigpestell, on 10/12/2007, -0/+4Persistent variables can be maintained through the $_SESSION variable, which can read from either server memory or client cookies, depending on the configuration.
- ahawks, on 10/12/2007, -2/+6Besides the fact that I haven't coded any PHP in quite a while... here's why I don't/wouldn't use OOP:
I had the word for it and forgot, but basically that PHP script instances aren't connected. ie: index.php runs, loads its objects, messes with some data, writes its page, etc, and exits. User clicks. account_settings.php loads its objects, messes with some data, writes its page, and exits. Whereas with Java/C++/etc, once you have an instance going, it stays going until the whole thing shuts down. - mikee7, on 10/12/2007, -3/+7yea, I use POOP too, but not LAMP. I'm forced to use WIMP (Windows, IIS, MySQL, PHP)
- LordLucless, on 10/12/2007, -0/+4@jasonok6: No, not really. If there are very few PHP jobs, and lots of PHP developers, then the PHP developer salary will be low because the developers have to compete to get the jobs; employers will take the ones who are willing to work for the least amount. By the same token, if there are lots of Java/.NET jobs, and the same number of developers as for PHP, their salaries are going to be higher because employers will have to compete against each other to attract talent. Supply and demand. You can't judge the relative merits of the language by looking at salaries in that situation; salaries are determined by economics, not technological superiority.
- sproutworks, on 10/12/2007, -0/+3You can easily store objects in sessions. You can also serialize them and put them in files. PHP 5 has special sleep and wakeup methods to help with this.
- flash200, on 10/12/2007, -0/+3@mcspectrum
Over-engineering can be worse than under-engineering. As long as sound programming practices are followed, like abstraction and modularity, under-engineering leads to a simpler codebase that's easier to modify.
If you wind up needing more functionally, you re-design later based on the new needs that arise, rather than trying to predict ahead of time what functionality will be added. - inactive, on 10/12/2007, -0/+3geronimo - "Not to knock PHP, but I see PHP as a scripting language that has been hacked together."
Absolutely not, php from the very beginning was designed to do one thing and one thing only...dish out webpages as fast as possible. It has succeeded 100% in doing that. Unlike Java, php wasn't designed to be the swiss army knife of programming to be implemented in any place that a processor could be found. I will be the first to say that the OO capabilities up to version 4 of PHP have sucked ass...but the core functionality of serving pages and serving them fast has always been there regardless of the shady OO functionality. - pixelmixer, on 10/12/2007, -0/+3Thats an interesting theory... I program PHP and I program/animate Flash Actionscript... But I cant get Java for the life of me... not sure why... I think its the lack of user friendly IDE's to use... The only Java editor i've come across that is just "Plug N Play" is Processing (http://www.processing.org)... I've done some things in Processing, but nothing that comes even close to the possibilities Java has (not saying that Processing cant do it, just I cant)... I've tried NetBeans or JavaBeans or whatever those are called but I could never figure the interface out... it just got in the way.. php is all web based.. you can do it with a server, a notepad and a browser.. I think its simple things like this that keep many PHP devs from going OOP... Flash/Actionscript is a different story.
- inactive, on 10/12/2007, -0/+3I agree completely, if I want to split a string, lower a string, search a string i will run the string through a function. Having the convienence of being able to call a method within the string object that does this for me is absurd. Most the time when using a string I am not going to manipulate it, thus the ability to do these things is costing me a lot in overhead. The idea that everything should be an object right down to integers and strings is stupid. It disturbs me to hear developers arguing over a language not being a "pure" OO language.
- thrasher6670, on 10/12/2007, -0/+3OOP IS better for large applications as well as complicated functionality.. if all you want to do is present some simple data to a user, fine a quick and dirty procedural script is great... something like say.. my website: http://gentoo-portage.com/ , you're going to need objects or the whole thign would be a big mess. Code here: http://source.gentoo-portage.com/
- danlemire, on 10/12/2007, -0/+2I found this article useful in helping me understand how to use objects in PHP. Using objects helps considerable in getting past the minutia of a website because you can infinitely reuse the code. The great advantages really show through when it comes to updating code. Instead of having to rewrite each and every php page, you can update the object. Provided that you don't change the fundamentals of how functions are variables are accessed and used within the object, the only changes you make are within the object itself.
And I have to admit with you quickly PHP is updated and changed, this is a great advantage. - kiranlightpaw, on 10/12/2007, -0/+2@SamKellet4:
This is exactly right. PHP has the most complete, accurate and usable documentation of any language that I've found. I've been so spoiled by PHP's documentation that it has made it difficult for me to learn other languages that lack similar documentation and user comments. - scottyallen, on 10/12/2007, -1/+3"In web apps, IMO, its not needed much."
Wow, you haven't written any large web apps, have you? I bet you still hand code every single sql query, as well (or don't write apps that require a database). Having worked on at least 5-10 large apps(20k+ lines) in a variety of languages, if you're not using objects at the persistence/business logic layer, there's something very wrong, and you're probably spending far more time in maintenance than you need to.
However, I'll agree that the front end dispatching system and controller code (if you look at it from an MVC perspective) doesn't really lend itself to OOP very well. But that's _very_ different from saying that web apps don't need OOP at all. - plasmatic, on 10/12/2007, -1/+3A lot of people use OO in php...
- vistic, on 10/12/2007, -0/+2This summer I started working on a project in PHP which is the first thing I've ever done in PHP. I went through and started coding based off of some examples I found online, but before long it seemed to be a mess to me. So I went back and re-wrote it using an object oriented approach, and it just felt a lot better to me (and was a lot easier to make changes to and everything).
My impression of PHP as an OOP language was that it is pretty passable... I mostly know Java, and this was like doing some basic Java stuff, except being able to do some really crazy things that would just be impossible or unthinkable in Java, thanks to th efact that PHP is sort of a scripting language too.
PHP is about as versatile as you can get as far as languages go. The language might end up looking sort of tangled to an outsider, but I found as I was coding, that if I could think of a way to do something, that PHP probably supports it. - inactive, on 10/12/2007, -0/+2jbarnett: I realize that their are some really good OOP implementations in the open source community...absolutely. However, if you are ever hired by a company to develop in php you are probably going to be working with existing code that is not object oriented. I have done a lot of php for companies and I have yet to work in an existing environment that was written using object oriented principles.
- geronimo, on 10/12/2007, -1/+3Not to knock PHP, but I see PHP as a scripting language that has been hacked together. Whereas languages like java were developed from the start to be nothing more than a tool to be used in any possibility. An object-oriented tool, which is key. It's hard to slap together OO functionality after you have an established language. It's possible to do it right but from the comments I see it looks like it wasn't done right. Perl didn't do it right, OCAML did it right.
That's why I like to use java, it has been from day 1 a good OO language. In fact people might (ab)use OO in the poorly-design OO PHP and think all OO is bad so there is a danger here. - mike503, on 10/12/2007, -0/+2procedural coding can be just as reusable. I think that OOP is abused a lot in PHP... I steer clear from it myself.
- discobean, on 10/12/2007, -0/+2I would really like strongly typed variables, and namespaces.
- Teratogen, on 10/12/2007, -0/+2PHP desperately needs namespaces.
- buss, on 10/12/2007, -0/+2I absolutely agree. PHP can and should use OO and functional code. I wrote a few objects to power things that I use many times over (PHP development at work), but in many cases objects just aren't needed and functional code is better. Lets all stop having this argument and agree that each method has its uses.
- brianlmoon, on 10/12/2007, -2/+4dumbest code ever:
$obj = new SomeClass();
$obj->execute();
I have seen that way too much. OOP is nice to have when it is needed. In web apps, IMO, its not needed much.
Having said that, if you can get away from getting sucked into the myths that OOP is better, PHP is the language to use for web development. - RyanJohnston, on 10/12/2007, -0/+2@saifatlast: I haven't played with objects in PHP recently enough to remember if $this is always required when you refer to a member from with-in a method. Assuming that it is, that is different from other OO languages like C++ and Java which only requires "this" to resolve ambiguous scoping (i.e. giving a parameter to a method the same name as a member in that class). Using "this" may make for cleaner code in those languages, but it isn't required.
- Electrawn, on 10/12/2007, -1/+2The author, before spouting off, should have looked at http://www.xoops.org . Plenty of OO PHP applications spring from Xoops.
Or perhaps a better metric, from http://sourceforge.net/projects/xoops/ , 99.99% activity, top 25 project rank.
Sheesh. - tyrione, on 10/12/2007, -0/+1Not yet having done any PHP I notice that class names are mixed and not capitalized first letter. class MyClass in ObjC/Smalltalk/Java/C++ seems to be written class myClass in PHP object syntax. What was the motivation of diverging from a common case structure that distinguishes class names from methods or functions?
- inactive, on 10/12/2007, -2/+3good call
- mikebritton, on 10/12/2007, -0/+1I use PHP objects all the time for AMFPHP (Flash remoting). It isn't a dying art.
- inactive, on 10/12/2007, -3/+4Dude, web applications are stateless...period. PHP, ASP.NET, Java...whatever you are working in...doesn't matter.
- cmallinson, on 10/12/2007, -1/+2"Dude, web applications are stateless...period. PHP, ASP.NET, Java...whatever you are working in...doesn't matter."
Sorry, not so. Don't know about other platforms, but in ColdFusion, objects can persist in the session or even application wide. Instantiated objects can persist accross multiple requests, or even users. The web "request" may be stateless, but the application most certainly is not. - lqqkout4elfy, on 10/12/2007, -0/+1JUST put that into a bit of code I wrote... I'm going to go to a corner and cry now... :(
- gunbuster, on 10/12/2007, -5/+6PHP objects still feel like a hack to me. We use them, but it still leaves me with a bad taste. I really dislike having to use $this every single time. At least they aren't as bad as Perl objects...
- blaksaga, on 10/12/2007, -0/+1People don't use OOP in PHP? That's news to me. I make a habit of using objects in all of my major php/web applications. I find it makes coding a lot cleaner.
- thinkdifferent, on 10/12/2007, -0/+1It can't really be a "lost art" if nobody was ever using it. Lost art refers to a technique that was at one time more common and the knowledge of that technique has been lost. Great example is stained glass.
- KicktheDonkey, on 10/12/2007, -0/+1I can't agree enough about the documenation for PHP...
Ruby? Crap docs.
Java? Crap docs.
Perl? The core language is not very good, but many of the CPAN modules are very well documented.
Python? Okay docs... They could use more examples and user contributed comments.
The documentation for PHP is what keeps people coming back to the language. I've done Java, and hated every minute of it. Doing something simple like opening a local file for reading and parsing required me to spend half an hour digging through books and web pages for that one little example that gave me what I needed. With PHP, I can simply google 'php open file' and the first result takes me to the PHP docs...
I know, if I coded day in and day out in Java, I'd pick up on these simple things. But when I need a web app coded for an ad hoc project in 2 weeks, I can either A) Spend 2 weeks laying the 'ground work' in Java, and be late or B) have it up in running in PHP on time...
Results count for more than elegant design... - Scarblac, on 10/12/2007, -0/+1Jasonok, _PHP_ is stateless. Java application servers and Perl (using mod_perl) absolutely are not (I have no professional experience with other servers, but most aren't "clear everything after every request").
Just that HTTP is stateless doesn't mean the server application is. - sonofagunn, on 10/12/2007, -0/+1@saifatlast - PHP forces you to use the $this notation inside member functions, whereas most OOP languages let you access member variables without explicitly referencing $this.
- sillywampa, on 10/12/2007, -1/+2@surfit
I don't see the logic in not using OOP for a web site. (you did say, for web scripting, I assume you mean for any web site). If you have more than one page and each one needs to access a database, having an abstraction layer to handle database calls is extremely useful. It keeps you from having to change your code in multiple places. I used to think like you, until I was getting tired of constantly coming up with kludges for my code. Having a class is just so much better. And I've worked on both simple small sites and extremely large, complex php sites. - inkubux, on 10/12/2007, -0/+1I use OOP in php basically in every app I build. If not how can you abstract totally your Sql and you Database Model to be reusable in many differents apps. You need to separate you app in some tiers Database/Rules/Gui if you want to create a reusable app. And without OOP tiers aren't very feasable.
- UrsusMorologus, on 10/12/2007, -0/+1I wrote some spamassassin plugins, which required oop perl. Nasty stuff. I've got one that's several k in size and I still don't know what's going on exactly.
I wrote some cacti plugings, which required function-oriented php. Very weak.
I can't find a good language that strikes the right balance between OOP and efficiency. Sucks to be me. - thrasher6670, on 10/12/2007, -0/+1you should also look into PDO.
http://ca3.php.net/manual/en/ref.pdo.php -
Show 51 - 75 of 75 discussions



What is Digg?