Jump to content
Killersites Forums

OO PHP class -vs- functions (in a single file)


Guest Mogul
 Share

Recommended Posts

Guest Mogul

I have been programming for over 15 years, but all the while have always used functions and haven't used OOP for anything yet. I understand the concept and how to create classes and call them, but what I am having a very hard time getting my arms around is how OO PHP is better than, and easier than a file containing PHP functions. (I would like to add that I am trying to understand and grasp this concept so that I can start programming in OO PHP)

 

Here's one scenario: I have created a handful (about a dozen) functions to connect to, insert row(s), update row(s), get single value, get array, etc. from a database. I have created all of these functions to be very generic so that they can be used them in many different projects. All of these functions reside in a single file (db_functions). So, any project/file that needs to access the database just "requires" this file and voila, all of the functions are available to be used within that file. Now, if I were to create a class of functions to do the same thing and the class lives in a single file and any project/file that needs to access the database will again "require" the file that contains the class this time and this time all of the methods will be available to be uses (called) within the file.

 

Using the above scenario, how is OO PHP any better than a file containing a bunch of functions? In both situations, all of the functions or methods are contained within one single file which is called at the beginning of the page/file that wants to use them. It just seems to be more cumbersome to use a class when the functions are right there. I just can't seem to grasp how these will differ. For my latest project, I am about to create a file to perform many tasks so that doing it my old way will have lots of functions contained within one file. These tasks or functions will be used on many projects in the future, hence the need to make these tasks modular and standardized. I hear and read that OO PHP is definitely the way that I should be designing this new project, however, I really want to understand and grasp the reason of WHY I am doing this.

Link to comment
Share on other sites

Hi,

 

I could go over everything point by point ... but what would be the point, given that I've covered much of what you want to learn about PHP OOP on killerphp.com.

 

;)

 

Please do the tutorial and let me know if you have any questions.

 

Keep this rule in mind: the more complex the project or the more people working on a project, the more useful OOP becomes.

 

Stefan

Link to comment
Share on other sites

OOP functionality is that you write modules that are standalone or co-modules that work as supplements to existing modules.

 

What this means is when you are cooding in a OOP-fashion you need to start thinking differently. And this is something I see people not doing as they simple put a bunch of functions and wrap them inside a class.

 

For example if you are writing want to create a good login system you are no longer writing a page filled with functions you will use specifically for your login system, but you are aiming at coding a class that is standalone and it so happens that this class can be used for the login system you are using.. but it also means it should work with anyother system that requires a login without needing specific code other than a childclass or a supplement class.

 

So what you are doing with OOP is creating lego blocks, after a time you have a whole bucket full of them. And when the time comes where you need to code something new you look in your bucket of lego blocks pick the ones you need and assamble them. Of course sometimes you notice that you need to change or add functionality what you do then is not go in and change in the original class but use the OOP way and write a child class with what you needed or write a supplement class this way you will create packages and you get new lego blocks in your bucket.

 

It's hard to view it this way but this is the goal of OOP and people need to start utulizing it this way.

And I'm sorry for any random strangeness in the text above after all it's valborg day in sweden and I'm drunk as hell .. but with the good amount of years coding I think my point is valueable!

Edited by krillz
Link to comment
Share on other sites

I think the main thing to keep in mind here is that just because you use OOP doesn't necessarily mean that you won't use regular functions or procedural PHP once and a while, or that you can't mix the two to some extent. Based on your explanation, it seems like you may be best served at the moment by keeping your single file of functions. If the functions aren't related to each other somehow, there's no point in placing them within a class and trying to use them that way -- that sortof defeats the purpose of OOP. Whatever functions/variables that object uses should be related to each other and the object itself should be able to stand on their own separate from the rest of the program.

 

For simple stuff, using procedural PHP often makes a lot of sense. If you're writing a one page PHP file that isn't complicated, it may be overkill to write something fancy using OOP. As Stefan said above though, OOP is extremely useful when you are dealing with complcated/large programs. Whether you use OOP or regular procedural PHP, pick the right tool for the right job.

Link to comment
Share on other sites

I agree with many points here and a lot of programmers code in OO style and forget why they do it. As its been said abstraction and modular development with re-usability the driving factor but these things get lost in an undisciplined environment. How many apps have we written where we have recoded our connection methods or query methods time and time again. I too fall into this trap way too often. If we were to plan an application properly we could start creating a suite of classes which theoretically could live above all our project directories and each project share each class as and when needed.

Link to comment
Share on other sites

I agree with many points here and a lot of programmers code in OO style and forget why they do it. As its been said abstraction and modular development with re-usability the driving factor but these things get lost in an undisciplined environment. How many apps have we written where we have recoded our connection methods or query methods time and time again. I too fall into this trap way too often. If we were to plan an application properly we could start creating a suite of classes which theoretically could live above all our project directories and each project share each class as and when needed.

 

But then again if we did that all the time we wouldn't have a job now would we as it would be converted to a one to two man tops position instead of having a whole team. I've been there a lot in the past but then you have to pitch why that old class can't be reused then finally spend time rewriting the existing class so the boss doesn't notice it's the exact same code.

 

good thing I moved away from being a full time coder for a living.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...