Okaaaaaaay....
I am migrating from PHP to C#, I used PHP because it was sooo easy to use and to understand, though I have always had a problem when it comes to understanding objects & classes, not how they are created but really, what their purpose is, I created many applications with PHP and found that I could create them using functions, and to my own detriment, I avoided classes because I couldnt really get on with them, Now I am migrating to C# for a number of reasons, mainly because if I want to get anywhere as a developer I MUST learn to use objects in there correct manner.
Also I know I am making references to PHP type syntax, but as I am moving to C#, if there are any crossovers, I hope you will forgive them!
I can understand that objects are very good for code reuse, but thats why i used functions, if I needed to describe a product, I created a function that would do this, then loop for each product in the DB, so If I have to create an object that holds a method to do this exact same job, why use the object instead of the method/function on its own.
I suppose these are a bit confusing to read, I am having difficulty articulating what I mean!
I know an object can be used to do what you need it to do, and I have tried reading through tutorials, in order to try to understand this, but I will try to put my point across and please let me know what I am confusing and where I am going wrong!
say I have the object cupOfCoffee, this can hold the values, milk(bool), coffeeType(string example: "instant"), sugar(int), container(string, example: "cup" or "mug")
would then if I was making an application that made multiple instances cupOfCoffee , if I used functions only would I get into trouble because to create multiple instances using functions I would have to store each cupOfCoffee instance either in an array, flat file or DB with a reference (so that they could be referenced individually), whereas using the cupOfCoffee Object, I could have multiple instances:
(psuedocode)
*****************************
jim = new cupOfCoffee;
sarah = new cupOfCoffee;
// now I have jim.cupOfCoffee, sarah.cupOfCoffee
//now I want to make it how they like it:
jim.cupOfCoffee.milk = true;
jim.cupOfCoffee.sugar = 0;
jim.cupOfCoffee.coffeeType = "Fresh";
jim.cupOfCoffee.container = "mug";
sarah.cupOfCoffee.milk = true;
sarah.cupOfCoffee.sugar = 2;
sarah.cupOfCoffee.coffeeType = "instant";
sarah.cupOfCoffee.container = "cup";
*****************************
Now, If was to do the same with Functions I would have something like:
*****************************
functon cupOfCoffee($name, $milk, $sugar, $type, $container)
{
$name = new array(); // I klnow this a PHP method, but I am just learining and this what I know!
$name[milk] = milk;
$name[sugar] = sugar;
$name[type] = type;
$name[container] = container;
$return name();
}
//now lets make the coffee:
$JimCoffee = cupOfCoffee("jim", 1, 0, "Fresh", "Mug") ;
$sarahCoffee = cupOfCoffee("sarah", 1, 2, "Instant", "Cup");
print_r (JimCoffee);
print_r(sarahCoffee);
*****************************
Both methods will return a value of jim/sarah, and the details holding their preferences of coffee, with the C# example they would be held in an object, in the PHP function they would be held in an array.
So what is the difference?
Why would holding the details in an object be better than holding the same details in an array? I'm sorry but I just cannot seem to get past the idea that you can get to the same result, I suppose the question is not why is it better, but why is it preferable to use and OOP than functions?
Or am I completley wrong in what I think?