Results 1 to 3 of 3

Thread: A question on object oriented programming

  1. #1
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default A question on object oriented programming

    I have been learning PHP for three weeks now and I am trying to understand Object oriented programming. I am learning about classes the last couple of days and I have a question...

    Below is the example code of a class given in php.net:

    Code:
    <?php
    class Cart {
        var $items;  // Items in our shopping cart
    
        // Add $num articles of $artnr to the cart
    
        function add_item($artnr, $num) {
            $this->items[$artnr] += $num;
        }
    
        // Take $num articles of $artnr out of the cart
    
        function remove_item($artnr, $num) {
            if ($this->items[$artnr] > $num) {
                $this->items[$artnr] -= $num;
                return true;
            } elseif ($this->items[$artnr] == $num) {
                unset($this->items[$artnr]);
                return true;
            } else {
                return false;
            }
        }
    }
    ?>

    And, an object is created from the class and used like this:

    Code:
    <?php
    $cart = new Cart;
    $cart->add_item("10", 1);
    
    $another_cart = new Cart;
    $another_cart->add_item("0815", 3);
    ?>

    Now, I see that all these codes do is, a function is created in the class and the function is called later...

    But couldn't we accomplish this just by creating a function alone? Why do we need to define a class? What is the advantage of it?

  2. #2
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    The example is too little to explain why, but I will try.

    First, the array is encapsulated within the class. You can create multiple copies of the class without having the need to duplicate the array (giving them unique array names) yourself.

    Second, you do not have to deal with the array directly. In other programming languages, there are other data types, such as linked list, rope, sets and etc. You can change the internal implementation of your class without impacting other code. That is also part of encapsulation.

    Third, is data hiding. The code snippet you are looking at is PHP4. With PHP5, variables inside a class can be made private

    PHP Code:
    private $my_array
    This enforces access to the array through the defined functions (or methods). This ensure that you know which functions are changing the array - if your array is being changed by any function, in any script, you got a headache. But if access to your internal variables are regulated by functions such as add_item(), and somehow you have a bug that the items are not being added correctly, you just have to look at add_item()

    Fourthly, it's polymorphism, but it is a rather advanced topic :P

    Fifthly, it's inheritance (just google "PHP extends class" or something). You can make another class inherit from an existing class to re-use its code, but provide specific functionality for the same functions or add new functionality.

    Sixthly, it's modular - data and logic goes together so that the logic would always have the data it is expecting, and both are as one unit. You can just take this class file and drop it into another PHP project and it works fine. In reality, of course, classes are more complicated than that and would need other classes to work. Is it possible to write single-dependency PHP classes for very low level functions, such as writing to a log file, calculations, reading files and etc.

    Hope this would incite your interest in it.

    Edit: Take your cart class, for example. All you need to get it to work is to include the PHP file and use its methods. You don't have to worry about adding to the wrong array somehow. Whenever you print the content of the cart, it is the correct one, not dependent on which array you pass into a function.

    It's modular because you can take this to another project and it works. This may not be true of strictly-functions based. First, where do you store the variables and still have data hiding? What if the global variable already exists for another purpose? How does a function ensures that it is getting the data it is expecting?

    Thirdly, you don't have to understand the entire code to get it to work. If you pass this class to me, with the example, all I need to know is that I can add and remove items. I do not care how it is done. If you give me a list of functions to include, I have to worry about what do I store my array, which function to use (you can use comments to tell me, but a public function explicitly means uses me!.

    You can also create multiple carts in the same php script too! Just by duplicating two or three lines of code.
    Last edited by CrazyChop; 04-06-2009 at 11:41 AM.

  3. The Following User Says Thank You to CrazyChop For This Useful Post:

    borris83 (04-07-2009)

  4. #3
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Thanks for the detailed reply...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •