chris wrote:
What is a class ?? is it like a function ??
this has allways confused me as i am a newby to programming (since Basic in
the 80's)
thanks for any insight you can give
OOP-101 :
Imagine that you have to work with a bunch of data.
Imagine that these data are organised in the same way, let's say that
each data item is made of a "name" field, a "descriptio n" field, a
"price" field etc..., all havings the same fields.
One obvious way to organise you data is to have each 'item' (one "name"
field + one "descriptio n" field + one "price" field etc) being an
associative array :
mydata1 = ['name' => 'yoyo',
'description' => 'A nice, fluo, glowing yoyo',
'price' => 1.49];
mydata2 = ['name' => 'PHP T-Shirt',
'description' => 'A nice, fluo PHP T shirt',
'price' => 9.49];
now you can use mydata1 or maydata2 etc a whole thing, and still
retrieve your datas...
Ok, now you're there, you may want to *do* things with your data, like,
say, displaying'em in a nice, fluo (err... sorry) web page. You could of
course code this data item by data item, but since *all* your items have
the same fields, it would be more simple to just write one function :
function printItem($item ) {
$buf = "Buy now our wonderful "
. <i>" . $item["name"] . "</i> : "
. $item["descriptio n"]
. " for just "
. " <b>" . $item["price"] . "</b><br>\n"
echo $buf;
}
printItem(mydat a1);
printItem(mydat a2);
But you don't just have to print your items, you have a lot of things to
do with them : reading them from a [DBMS|XML file|csv file|...], storing
them to a..., etc.
So you have a whole lot of 'doSomethingWit hMyItem' functions to write.
So far so good, you've defined what you could call an 'abstract data
type' - there is much more than this to ADTs, but well, you've got the
point.
Now imagine that instead of having your 'data type' (the associative
array) and a bunch of functions taking one of those 'data type' item as
an arg, you just add the functions *into* (well, that's not how it
works, but that's how it looks like) the associative array, and give a
name - say 'Item' - to that data type. Now you could for exemple rewrite
your code like :
class Item {
$var name;
$var description;
$var price;
// this function lets you initialize an Item
// when you 'create' it.
// Note that the '$this' thing is a reference to
// the specific Item that you're working with
function Item($name, $description, $price) {
$this->name = $name;
$this->description = $description;
$this->price = $price;
}
// Note how the '$item' param in the previous version
// is now replaces with the '$this' reference
function print() {
$buf = "Buy now our wonderful "
. <i>" . $this->name . "</i> : "
. $this->description
. " for just "
. " <b>" . $this->price . "</b><br>\n"
echo $buf;
}
}
mydata1 = new Item('yoyo', 'A nice, fluo, glowing yoyo', 1.49);
mydata2 = new Item('PHP T-Shirt', 'A nice, fluo PHP T shirt', 9.49);
// NB : the 'print' operation has been defined
mydata1->print();
mydata2->print();
Well, basically, that's just what it is.
A class is a way to define a named data type, with a data structure and
some operations working on that data structure.
Then you can create 'objects' (or 'instances') of that type, and call
operations on them.
Now there is more than this in classes and objects, but I'll let you
discover what... !-)
HTH
Bruno