advantages of classes over just functions?

Discussion in 'PHP' started by liam_d, Dec 9, 2005.

  1. #1
    Well im looking over forum code and comparing it to mine, currently looking at icebb.

    What is the advantage of this;

    $mywrapper->message($lang['title']['no_post'],$lang['text']['no_post']);

    over this

    message($lang['title']['no_post'],$lang['text']['no_post']);

    ?

    (note this is just as an example not proper variable names and such)
     
    liam_d, Dec 9, 2005 IP
  2. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #2
    From the PHP5 book ;)

    It allows for the encapsulation of code, creating an enviroment where you can protect member objects and properties from being modified outside the class.

    Now in a PHP 4.x.x enviroment it is more of a grouping of functions then anything else.
     
    palespyder, Dec 9, 2005 IP
  3. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Whats the difference between OOP is 4 & 5 then, my knowledge isn't great im still a basic php guy really i only thought OOP was functions and classes?
     
    liam_d, Dec 9, 2005 IP
  4. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #4
    It would probably be easier to send you here

    PHP5 Classes

    This will explain the main differences. PHP is moving to a more "true" OOP model.
     
    palespyder, Dec 9, 2005 IP
  5. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If done right, OO code will promote code re-use and provide a framework for building large applications. There is no specific advantage to using objects over associative arrays that I know of. In fact, I would think(not sure about the stats in PHP) objects require more overhead and I might think twice about using objects if I was not using the benefits of design patterns, polymorphism, etc.

    -phil
     
    philej, Dec 9, 2005 IP
  6. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here's another concrete advantage even in PHP 4:

    Say you have a bunch of functions that refer to each other and pass around pretty much the same arguments. That's a good sign that those functions belong in a class, and those arguments should be members of the class.

    Passing around fewer arguments is a Good Thing. It makes your code easier to read and debug.

    Of course, quite a few people disapprove of developing software in this manner. Do all your design work up front and work out any kinks before anyone writes a single line of code. Then again, I suspect most of those die-hards just have the C programming mindset and don't get the point behind higher level languages.
     
    jimrthy, Dec 9, 2005 IP
  7. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    reuse, reuse,and more reuse

    objects are alot more logical, also if you ever did OO programming ull never go back to iterative paradigm

    between PHP4 and PHP5 the main differences that spring to mind are private/public mehods

    to be honest with you PHP5 stll have long way to go before it looks as sexy as full OO languages like Java, i mean that uggly dollar sign $ has to go!
     
    cornelius, Dec 11, 2005 IP
  8. MAtkins

    MAtkins Member

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    I rarely see the advantage to using classes in php, especially for use of 'static' functions.
    I agree that if you're using the same variable names for different functionality it makes sense.
    I name my variables very distinctly so I know what they mean.

    When you have situations where you need more than one of an item that has multiple variables, like a 'company', classes can come in handy. For example, I could have an array of 127 'company' objects, all derived from my 'company' class, containing all the data that I need for each company. Each company object could even potentially inlcude an array of 'employee' objects, derived from an 'employee' class.

    i also think that if you're using multiple threads then *sometimes* classes can help with thread safety.

    For people who have trouble keeping mental track of code, classes can help organize it.
    I generally create files that would normally represent classes, but don't make them actual classes.

    Overall, classes take up memory (just a tad) and make calling functions more difficult.

    I completely agree that the $ has to go - it drives me nuts.
     
    MAtkins, Jan 20, 2011 IP
  9. robsnob

    robsnob Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It's all about separating functions into logical objects.

    And even separate classes with namespaces ..

    Think of the following example: Normal functions
    
    
    $app = null;
    $car = null
    function setup(){
       global $app;
      // Do stuff to setup an application
    }
    
    function car_setup(){
       global $car;
      // Do stuff to setup the car...
    }
    
    funciton car_changeGear($gear){
       global $car;
       // Do stuff to change gear on a car.
    }
    setup();
    car_setup();
    car_changeGear($gear);
    
    PHP:
    In an object oriented way, we can separate the functions in a class, we can now separate it more clearly!

    
    
    class Application{
    
        public function setup(){
            // Do stuff.
        }
    
    }
    
    
    class Car{
        public function setup(){
            // Do stuff.
        }
    
        public function changeGear($gear){
            // Do stuff.
        }
    
    }
    
    class ShinyCar extends Car{
        public $hasNiceShinyBody = true;
    }
    
    
    $app = new Application;
    $app->setup();
    
    $car = new Car;
    $car->setup();
    $car->changeGear(2);
    
    // We can now create more cars (with their own object)
    $car2 = new Car;
    $car2->setup();
    $car2->changeGear(2);
    
    // And we can create another car, but a shiny car like this
    $car3 = new ShinyCar;
    $car3->setup();
    $car2->changeGear(5);
    
    
    PHP:
    Do you see what is more clean? We don't have to use global variable or pass a variable to the function because we have created an object.
     
    Last edited: Jan 20, 2011
    robsnob, Jan 20, 2011 IP