Welcome Guest, Not a member yet? Register   Sign In
Objects: Why are they useful?
#5

[eluser]ejangi[/eluser]
Personally I think to say that an object is a collection of functions simplifies it too much. The idea of objects is to describe (in code) things more like we experience them outside of code.

Take a hammer for example, it has properties:
- material: metal
- weight: X kg/lb

And it has "functions":
- hammer nails
- remove nails

You can actually describe this "Object" as a class:
Code:
class Hammer {
    var $material = 'metal';
    var $weight = 1;
    
    function Hammer($material = 'metal', $weight = 1) {
        $this->material = $material;
        $this->weight = $weight;
    }
    
    function hammer_nails() {
        // Do functionality here
    }
    
    function remove_nails() {
        // Do funcationality here
    }
}

This class is like a cast or a mould that you can form objects out of. So to create a new hammer object, you would use the following:

Code:
$my_hammer = new Hammer();

We can also make different variations of the object as well:
Code:
$my_other_hammer = new Hammer('wood', 2);

Classes "can" be used to namespace functions (as tonanbarbarian explained), which is probably the way CI uses them mostly, but to me the example above fits more closely with the intention behind classes/objects than just "containers".


Messages In This Thread
Objects: Why are they useful? - by El Forum - 01-22-2008, 02:59 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:18 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:32 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:02 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:20 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:22 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:31 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:38 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:41 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:49 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:52 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:01 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:06 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:19 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 06:55 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:15 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 08:15 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:47 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:59 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 06:34 PM
Objects: Why are they useful? - by El Forum - 01-25-2008, 02:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB