OOP in a web application

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

OOP in a web application

Post by The_Anomaly »

I was debating whether or not to add this to the Design and Theory forum, but since it's such a noobish question, I'll ask it here. Perhaps my google-fu is weak, or I'm not looking in the right places--but I have thoroughly explored this before asking it on here.

With that said, I'll present my problem/question. I used to be a Coldfusion developer for several years, and now I've moved to PHP. I simply love the language and everything about it--but more than anything, I love the OOP. The book that got me started taught the OOP topics thoroughly, and gave me a good understanding of the concepts. However, what it did not even come close to teach was how to implement those concepts in a web application.

Basically, how do I apply OOP concepts to a general web application. By general web application I mean something like a medical records program--or a video sharing site, sites that have to do with the database CRUD, heavy validation, even social networking at times.

Could someone help me out here by telling me how to apply the OOP concepts that I've studied in a business web application?
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: OOP in a web application

Post by it2051229 »

yeah.. you create classes of some kind that you can re-use. like for example, a class that you just call directly when you want to connect to a database. generic classes that you can make use of like encrypting passwords, resizing images, validating fields, and etc. etc... OOP minimizes the development time. Instead of recoding everything, you just include the classes you need and use it on your program.
mach
Forum Newbie
Posts: 5
Joined: Thu Aug 14, 2008 8:10 am

Re: OOP in a web application

Post by mach »

Well if I was just beginning with OOP in PHP I would use ZEND framework (just google it). It enforces the MVC model and thus you will simply have to use OOP appropriatelly.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: OOP in a web application

Post by pickle »

As the above post showed, you're going to get lots of people pushing MVC. Don't let them. MVC might have it's place, but it's not every place.

How to use OOP? It really & truly depends on the situation. The only purpose of OOP is to make the code easier to use. The idea is that if you use properly designed classes, the code will be cleaner & easier to maintain. For example, if you use a class to connect to a database, then you change your database engine, you only need to update your class rather than pouring through all your code updating each database call.

My approach, which is likely not the best for every situation but usually works for me, is to layout your application first. Do an outline of all the functionality & actions that you'll have to do. Once you do an outline, look at the functionality & see if there's any common ground. For example, if you know you're going to have to be able to create, edit, and delete ... restaurant reservations for example ... it might make sense to make a Reservation class.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mach
Forum Newbie
Posts: 5
Joined: Thu Aug 14, 2008 8:10 am

Re: OOP in a web application

Post by mach »

What pickle says seems logical and in fact it is exactly the approach I have been using. Unfortunately, after dozen or two of larger projects, each using similar but somehow different, (well even improved) sets of classes, interacting in similar but slightly different ways, I found myself in something very similar to a maintaince hell. If I have the luxury to start from the scratch, I would make very sure that I use a framework (even if it ads some extra overhead). I would also use standard design patterns wherever possible (even it sometimes means more code or less elegant solutions).

My two cents.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: OOP in a web application

Post by Luke »

The only purpose of OOP is to make the code easier to use
I don't agree with that. Easier code is one things that OOP can accomplish (but sometimes doesn't). The main goal of OOP is code reuse and modularity, not usability. There is no ONE purpose of OOP. It has lots of purposes.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: OOP in a web application

Post by pickle »

If you can re-use code, say a function to do an sha256 hash of a string, don't you find that easier than having to rewrite the algorithm every time you need to run the hash? Code re-use is almost always for the programmer's benefit, not the program. While it might have benefits if the compiler is written properly, usually that's not why people tout code re-use.

Modularity also does absolutely nothing for program efficiency. It's entire purpose again, is to make life easier for the programmer.

I'm open to discussion of course, but I'm sticking by my statement. In some respects, the most efficient script is procedural & in one file - everything else that's done (building function libraries & different classes to do different things) is done entirely to make life easier for the programmer.

Don't get me wrong - I love OOP & use it everywhere it makes sense. I'm just not of the belief that it benefits the execution of my program beyond the fact that it reduces stupid mistakes I make.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: OOP in a web application

Post by Luke »

Hmm... yes I suppose you're right. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP in a web application

Post by Christopher »

pickle wrote:I'm open to discussion of course, but I'm sticking by my statement. In some respects, the most efficient script is procedural & in one file - everything else that's done (building function libraries & different classes to do different things) is done entirely to make life easier for the programmer.

Don't get me wrong - I love OOP & use it everywhere it makes sense. I'm just not of the belief that it benefits the execution of my program beyond the fact that it reduces stupid mistakes I make.
I think you are probably much more wrong than right. In theory what you say makes perfect sense -- but then there is that whole reality thing. The problem with your statement is that, in general, well designed programs perform well. And the problem with Procedural is that it immediately imposes a set of design constraints on you.

Sure the the parts may perform well initially or the whole application when still small. But real applications grow and get older. My experience, in reality, is that OO application tend to maintain their performance while Procedural applications tend to lose their performance over time. That's because OO and modularity are not "to make life easier for the programmer" -- they actually make it possible to add to and modify complex applications. They were created because un-modular procedural applications are inherently difficult to tune, maintain and improve.

You may think that modularity has nothing to do with performance, but even trying to tune a complex application that is not modular is very difficult and time consuming. The idea that the most efficient script is procedural and all-in-one file is a fine theory, but is simply not a reality for anything other than the simplest scripts. On top of that, as complexity increases putting everything in one file may start to hinder performance in PHP.
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: OOP in a web application

Post by pickle »

I think we're getting off-topic here, so I'll just PM you.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP in a web application

Post by Christopher »

Not sure it is that off topic for such a general question. The why is a important as the what. The initial post asked specifically asked about building "medical records program--or a video sharing site". With any bigger-than-one-script applications in PHP, I would recommend building a Domain Model first. The rest of the application will flow from that because the rest if the application is all about viewing and modifying the Domain Model.
(#10850)
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: OOP in a web application

Post by The_Anomaly »

EDIT: Sorry for taking so long to get back to this thread. We've been out of the country, and the traveling didn't allow me to respond to this thread.

mach: I never had seen that framework. From what brief look at it I took, I really did like it. As you said, it does press some of those concepts of OOP, and it comes off much cleaner than my current implementation of MVC.
My approach, which is likely not the best for every situation but usually works for me, is to layout your application first. Do an outline of all the functionality & actions that you'll have to do. Once you do an outline, look at the functionality & see if there's any common ground. For example, if you know you're going to have to be able to create, edit, and delete ... restaurant reservations for example ... it might make sense to make a Reservation class.
That makes a lot of sense. Actually, just taking that approach showed me a couple of opportunities where I could use this type of OOP. So, that helps.
With any bigger-than-one-script applications in PHP, I would recommend building a Domain Model first. The rest of the application will flow from that because the rest if the application is all about viewing and modifying the Domain Model.
I'm not really sure what the domain model is. Unfortunately, my googles didn't turn up anything really good for PHP domain model. What exactly is the concept behind it? How is it different from the MVC model? Could you give me a link or two for it?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP in a web application

Post by Christopher »

The_Anomaly wrote:I'm not really sure what the domain model is. Unfortunately, my googles didn't turn up anything really good for PHP domain model. What exactly is the concept behind it? How is it different from the MVC model? Could you give me a link or two for it?
A Domain Model comprises all (or most) of the Model objects in the Domain/Business Layer. The idea is first to create the Models of the data and their relationships to each other. The you build the rest of the application to present and modify that data.

http://martinfowler.com/eaaCatalog/domainModel.html
http://www.martinfowler.com/bliki/Anemi ... Model.html
http://www.google.com/search?hl=en&q=do ... h&aq=f&oq=
(#10850)
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: OOP in a web application

Post by it2051229 »

what the hell.. if you want know OOP read a book.. ther're like a lot of them in google and wikipedia..
Post Reply