Classes and Objects vs. Arrays

Discussion in 'PHP' started by sudowin, Oct 26, 2011.

  1. #1
    I'm learning PHP.

    Are classes and objects really that useful? Or are they just another way of creating and using arrays?

    <?php
    
    class Bird
    {
        function _construct($name, $breed)
        {
            $this->name = $name;
            $this->breed = $breed;
        }
    }
    
    $tweety = new Bird('Tweety', 'canary');
    
    printf("<p>%s is a %s.</p>\n", $tweety->name, $tweety->breed);
    
    # VERSUS
    
    function create_bird($name, $breed)
    {
        $new[0] = $name;
        $new[1] = $breed;
        return $new;
    }
    $tweety = create_bird("Tweety", "canary");
    echo "{$tweety[0]} is a {$tweety[1]}.";
    
    ?>
    PHP:
    The reason I ask is because I can't get this class thing to work.
     
    sudowin, Oct 26, 2011 IP
  2. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should look at a thread I recently posted:

    http://forums.digitalpoint.com/showthread.php?t=2310004

    I am about three years into PHP now.. probably a year and a half of really digging in.

    I have done and written PHP in every possible way wrong and still made things happen, my concentration lately has been to finally take the jumbled mush of knowledge in my head and start working it into efficient good code that is easy to use and re use.

    Rukbat I think puts a good explanation in my thread about when to use classes and objects (object oriented PHP) and when to use functions and the like (procedural PHP)

    I think (hope??) most would agree that to learn PHP, classes and object can come at a later time when you have a good or somewhat of a grasp on the other stuff.
     
    absentx, Oct 26, 2011 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #3
    In your very basic example there isn't alot to gain from using a class but they really are useful.

    I'd doing some cakephp work at the moment and every class calls $myObject->save($data)

    That's all I have to remember. However when I look at the class definition I will see different validation rules and in some cases I call the parent rules as well, in others I throw them out and start over. If I had this code in functions I'd have to remember which save function to use for each type of data and that would slow development down. I have 50 tables so that would be 50 different save functions.

    I have "content" on the site but there are different types. So I have news, events, and items - all stored in the same table and all essentially the same. There is little in the class definition of news, events and items because they extend content which has all the logic but where something different is needed I can make that change. Plus they all have their own "views" because the layout and labelling is very different (which is the beauty of MVC).

    Really you have to look at your project and decide the best approach. Once upon a time I went mad with classes which weren't really necessary, now I'm a bit less likely to use them for a simple page. It all depends on the reuse requirements and security.
     
    sarahk, Oct 26, 2011 IP
  4. sudowin

    sudowin Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok sounds like classes are something I should learn about later on in my PHP studies. Thanks for the feedback! I'll definitely look back at this thread when I'm a little more advanced.
     
    sudowin, Oct 27, 2011 IP