Jump to content

How do you know when to convert a site into OOP?


kratsg

Recommended Posts

I've read through this forum and can't quite get a clear answer.

 

If a site has relied on functions in a page that was included on other pages... Does that mean it's beneficial for that site to convert to OOP so they can control the functions much more easily? (It's hard to explain, but you should get what I'm asking about).

Link to comment
Share on other sites

Some (myself included) would say that it is almost always beneficial to program in OOP. That being said, you should never have to rewrite your classes either. Once you have a good framework to build upon your life becomes just that much easier.

 

I only ever implement temporary / quick fix solutions procedurally. Never would I bass a site around procedural code.

Link to comment
Share on other sites

Is OOP really a matter of balancing demand from customers and benefits for client?

 

It seems that it's almost always better to program in OOP whenever possible, just because you can create multiple objects that store different information that process it similarly as well as the logical side of it, making it easier conceptually. It has been said that there was no difference in speeds or actual coding between procedural and OOP (but I think it may be wrong... OOP may be slightly faster?)

Link to comment
Share on other sites

t has been said that there was no difference in speeds or actual coding between procedural and OOP (but I think it may be wrong... OOP may be slightly faster?)

 

I think you'll find its the opposite. Instantiating an object actually costs time. That being said, you really won't notice the difference until your applications become quite complex, and even then there are things like memcache to help you out.

 

The main benefits of programming in OOP are if its done correctly, it should be quite easy to add functionality to an application. Without the need to hack the application to pieces. That and of course the whole code re-usabilty thing.

Link to comment
Share on other sites

So for the most part, OOP is best used with sessions if you want to pass data over to another page correct?

 

I can agree with creating an object would take more time cause who the hell knows how big the object CAN get?

 

As far as keeping your classes on files, is it best to put one class per file or all classes in one file? I was leaning towards one per file, but that's a lot of files you would have to include if you needed to get 3 or 4 classes as compared to including all the classes in one file. But PHP is one that reads the whole file when it's included so if having all the classes in one file actually makes it a slower load, wouldn't it be better to separate them among files?

Link to comment
Share on other sites

So for the most part, OOP is best used with sessions if you want to pass data over to another page correct?

 

I'm really not sure what your trying to get at here.

 

is it best to put one class per file or all classes in one file?

 

I always have my classes in there own file, and usually use the __autoload() function to load them as required.

Link to comment
Share on other sites

Auto-load sounds like something I would use...

 

What I was talking about was let's say you go to different pages, like a shopping cart. On the items catalog, you click an item to add to your cart, this would (most likely) post to the same page, reload and run the class for adding an item to the cart. Then, you go to the checkout page, how can it determine exactly what items are in your cart unless you use sessions to keep track of the userid or w/e?

Link to comment
Share on other sites

Auto-load sounds like something I would use...

 

What I was talking about was let's say you go to different pages, like a shopping cart. On the items catalog, you click an item to add to your cart, this would (most likely) post to the same page, reload and run the class for adding an item to the cart. Then, you go to the checkout page, how can it determine exactly what items are in your cart unless you use sessions to keep track of the userid or w/e?

 

Yes, you would need to use sessions, cookies, or a database depending on how long you want the items to stay in the cart.

Link to comment
Share on other sites

Hi, I don't mean to hijack this thread, but my question's are somewhat similar to kratsg.

 

I have been programming for about a year, and pretty much just jumped straight into OOP, I don't really understand why you wouldn't.

 

I have been making myself a framework over the course of stuff I've done and notice I use globals allot and that it can become messy and sometimes difficult to see what belongs to what.

 

Generally all my php files are classes and I use a function at the bottom to call a process function.

I generally have a central class with all the main functions I use everywhere and I usually have a class sorts out what is displayed to the user.  Then all other classes are called by sorting class and do various things.

 

What my question is, is what is the best way to be passing information around instead of having lot's of globals and particularly in reference to when a button in a template is pressed?

 

I hope this makes sense, if not I can post some examples of my shoody programming skills to help explain.

 

Thanks

Link to comment
Share on other sites

In response to that, I have had that problem, initially when I started in OOP (seems common).

 

What I decided ultimately was to look at my different objects, go "hey! Wait a minute, I don't need to use two different objects, I can use just one!" So I kinda went on the idea that if you have a function that's used a lot, try separating into two objects, under the same function name. It helped me personally, because then I would include just the one class, instead of a parent class, and a specific class. I dunno if this makes sense, but OOP is not about... speed or efficiency, but rather a conceptual model for you. However it's set-up should make sense to YOU and other programmers.

Link to comment
Share on other sites

Hmm, not sure if you understood what I mean.  I do that anyway.  But I suppose thats what I'm trying to do in the case laid out below...

 

Basically, for passing $vars between methods in different classes I use globals.  This can become messy especially when trying to work out whats available to what.  Especially when stuff gets passed thought one method to get to another, which I don't think return can be useful here.

 

Also I usually have it set up so that my form data is usually passed through a handler class that passes data onto child classes.  Which generally handles through switches.

 

Is there a better way about going about that?

Link to comment
Share on other sites

A properly designed program, whether procedural or object oriented, will have very few if any global variables.  If you are placing global variables all over the place, you are not doing things correctly.

 

OOP is very difficult to teach.  The ideas are very easy to understand, putting it into practice is the difficult part.  You can only learn this through experience.  The best way is to constantly rewrite what you have.  Create a simple OO piece of code.  Step back and look at it.  What did you do wrong?  What did you do right?  How could you have written it different to avoid the things you did wrong?

 

Answer those questions and rewrite it.  Then repeat.  When you think you no longer did anything wrong, post it somewhere for peer review.

 

You don't need to write a complex project to do this.  You could create a system for logging, database management, sessions, etc.  Take a small component of a larger system and practice with it.  It's the only way to learn.

Link to comment
Share on other sites

Lol, Thanks Roopurt18,

 

That's great advise, I suppose I should have just gone on with that myself.  I do however would like to know if this is common place.

 

I have two switches in my handling class.  One that deals with _GET data from various hrefs and generally just present the correct template.

 

Then another that deals with buttons, and that usually opens up a script, which then displays the correct template after doing something.

 

Would it be better to just use buttons, styled to look like anchors (except when u do actually want an anchor)?  And is the scenario I'm using at the mo common?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.