tags:

views:

169

answers:

5

if i have a collection of useful functions in a file called functions.php (easy enough) what would be the advantage of OOP?

I want to learn it, but its pretty in depth so before I dive it, would be nice to know if there is an advantage. I can send variables to the functions just as easily as I can receive them.

example i have functions del_some_string($x,$y)// made up function to delete y number of letters from $x

why would this be better as a OOP?

+2  A: 

Depends on what the functions do, if you have some common data on which you do various operations to manipulate the same data you might want to model it in a class. On the other hand if all data you are working with has no relation with each other there is not really a benefit of using OOP.

For example the function you mentioned is more a utility function than a member of a class.

Tomh
Then again, if you have a group of string functions, you could make a String class that has static string function. This would act as a library.
Chacha102
all I really write in PHP are webpages. if you visti my page you'll see that while they work well, i can assure you they have probably the messiest code around LOL
@Cacha102, you have a point and I agree to a certain extend, but I would not consider that a shift towards OOP as you basically use it as just a namespace to group that functionality.
Tomh
+1  A: 

OOP goes much further then just calling functions.

Classes collect functions that operate on the same set of data or share the same goal. But another advantages of classes taking DRY to the next level. With functions, you can encapsulate some simple task so you don't have to repeat that over and over. With OOP, you can encapsulate more complex tasks so you won't have to repeat that.

If you tend to pass the same sort of data to several functions sequentially, you can think about using a class, and pass that data to the constructor and saving it in the class. That way, you can call the functions without having to pass that data each time.

Ikke
I used to write heavily in pascal and I ran across the same issue functon "uses" vs oop. OOP seemed to be like a loot of work to do simple things, yet everyone was doing it.
"OOP: making simple things complex to make complex things simple". OOP can make some things more complex, but if applied correctly, it can gain a lot too.
Ikke
+1  A: 

I've been using PHP for roughly 10 years now. I can't count how many times I've run across "functions.php" or "library.inc.php" or one of its many equivalents.

It's a bad idea. Don't do it. If you find yourselfs writing files like that, stop it. NOW.

It's not a matter of OOP vs functional programming. It's a matter of organising your code. Group your functions and variables properly and OOP will feel natural. If your functions do many things and don't lend themselves easily to being grouped, try to refactor them so they only do ONE thing at a time. You'll find that this also lets you strip a lot of redudancy out of your existing functions.

You can use OOP to encapsulate your code further: put your variables into objects and have the functions that operate on these variables either be property methods of those objects or interact with these objects as input. Avoid "global" and don't modify your function arguments routinely.

Your problems aren't the problems you think you have.

EDIT: As for your example: since strings aren't objects in PHP, you can't really put that function in a sensible class, so you'd probably want to put it into a utility module with other string functions and use it as you already do. Still, tearing apart your functions.php and turning it into a collection of atomic modules does wonders to your code's readability. You may find a file containing assorted functions readable because you are familiar with its contents, but another coder (and "you after three months of not using the code" IS another coder) will not.

EDIT2: Please understand that "OOP" doesn't magically make everything better. It's a tool that is supposed to help you writing better code. If you use OOP everywhere, you're probably doing it wrong (or writing for Java *cough*). If you use OOP without understanding things like structured code first, you won't get anything out of it, either.

Alan
I agree with you 100%. there is an old saying "... in times of stress, people resort to habit!" LOL Thats me. I want to STOP! but now sure how to convert what I know to OOP :-) That is where my problem lies. I tried last night. made a few classes. a little klunky, but I think i can see how it can work. bad thing is i simply moved my funcitons to the class. I am pretty sure that is really not the proper way LOL - any suggestions for the migrate to OOP?
You need to structure your code first. OOP can't do that for you automatically. The problem with OOP in PHP is that in many cases it's not at all the best solution to the problem. I found that if you have ORM and a template system and follow an MVC architecture, you don't really need any special classes. I don't really like most PHP frameworks, though, so in this case I stuck with Smarty and a spartan ORM called `PORK.dbObject`.
Alan
A: 

You can instance of a PHP class and use it's all combined properties and methods. You can manipulate an object during the execution of the script, and state is saved.

class Myclass{
  public $a, $b, $c;

  public setA(){
    $this->a = b;
  }
  public getA(){
    return $this->a;
  }
}

this is what you cant do with a function:

$x = new Myclass;
$x->setA();
print $this->getA();

you can't simulate the same functionality with a functions PHP. You run your functions one time, and since there is no instance, you have to hold these variables somewhere else. If you write a big aplication, this functions.php will be a big black hole. If you use seperated classes with it's functionality-aimed properties and methods, you can extend your PHP application as much as you want. If you don't use OOP, you can never setup a framework. but you will understand the importance of OOP, when you have to write something huge.

eyurdakul
Indent your code 4 spaces to preserve formatting, or, highlight it and click the code icon (1010101) in the editor.
Erik
A: 

It wouldn't. It's not a matter of better or worse. It's the paradigm that's important.

All computer programs aim to solve a problem. You can use Imperative Programming or you can use OOP (among others) to solve this problem. OOP will just change how you structure and organize your application, because it embraces concepts like encapsulation, polymorphism, and inheritance (to name some). These are not generally available in imperative programming, but that doesn't mean imperative is worse. It's just different.

If you are coding PHP, you can use either or. PHP is not a strictly object-oriented language. However, as of PHP5, there is an increased focus on OOP. New additions to the API are almost always Classes now (SPL, DateTime, Internationalization). If you want to use those, you definitely should look into OOP.

Gordon