3

I have just started studying OOP and I am a little confused of the benefits of doing either functions or OOP. I understand OOP is much more versatile in organizing your code, but for example, I have 50 lines of code, that occur on 7/10 pages of my site. Is it better to create a file and put a function in the file, and then use an include, or do the same with a class? Furthermore, is it better, to mass classes into one file and include it? If anyone has a bit of insight that may help clear up my confusion, I would really appreciate it.

2
  • 1
    Just a note, just because you put functions into a class as a "collection" for it does not necessarily make it "OOP" programming. It would be more or less looked at as an easier way to look at / manage code. Commented Sep 21, 2010 at 22:38
  • 1
    @premiso, well said. Neither OO or procedural programming automatically gives you organization. You can write (un)organized code regardless of the style. Commented Sep 21, 2010 at 22:48

3 Answers 3

3

The appropriate metric is usefulness.

If this code does just one thing, accepts a few input parameters, and returns a single result, then it's a function.

If the code can collect data, and does something tricky on this data, and you can specifically reuse results or get different results, then turn it into an object.

Sign up to request clarification or add additional context in comments.

Comments

2

If you have a 50 line function and that's all there's nothing wrong with putting it into a file and including it on your pages.

The benefits of OOP come when you have more code. And it's standard practice to have one class per file.

2 Comments

is it considered inefficient, to have multiple includes like this, for example, for the footer or header or some other simple information that reoccurs as well since it creates more http requests?
no. the time it will take to include another file is so minimal it doesn't matter. The maintenance boost you get from doing so would make up for it anyway. imagine if you had to change the header and footer and you had to do so in 100 files as opposed to 2.
1

Almost all "utility" functions can be grouped together by functionality. You start off with one or two... and end up with a file full of a hundred miscellaneous functions. So I would never use global functions for reusable code. Instead, consider:

<?php class Group { static public function func1() { } static public function func2() { } } Group::func1(); ?> 

In PHP 5.3, you can use namespaces instead:

<?php namespace Group; function func1() { } function func2() { } ?> <?php // from a different file / global namespace: Group\func1(); ?> 

Now regarding using classes vs functions... It's simple. If some group of functions "own" data or need to remember some sort of state, you almost definitely want to use a class.

2 Comments

Thanks for your response, when you say own data, or need to remember a state, what exactly do you mean. Can you maybe post an example? Thanks again.
It's hard to give a good explanation without going into a lot of detail. You may want to search this site and others for the general "why / when to use oop / classes" question. But think of it this way: if you access global data from within a function or pass large/partial arrays around from function to function, then you probably should be using classes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.