borris83
04-06-2009, 11:19 AM
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:
<?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:
<?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?
CrazyChop
04-06-2009, 11:36 AM
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
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.
borris83
04-07-2009, 03:11 AM
Thanks for the detailed reply...
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.