By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

maybe a stupid question regarding classes

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
NK
I loved BASIC :)

Check out this tute, I found it very helpful, written in an easy to
understand manner and it will make a LOT more sense of what a class is :)

http://codewalkers.com/tutorials/54/1/html

Cheers,
NK

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

Jul 17 '05 #2

P: n/a
"chris" <so*****@here.com> wrote in message
news:<3f********@funnel.arach.net.au>...

What is a class ?? is it like a function ??


The PHP Manual says:

A class is a collection of variables and functions
working with these variables.

http://www.php.net/oop

You can also think of a class as a comlpex type, something
like an array, which, in addition to variables, may include
functions.

Cheers,
NC
Jul 17 '05 #3

P: n/a
"NK" a écrit le 11/12/2003 :
I loved BASIC :)

Check out this tute, I found it very helpful, written in an easy to
understand manner and it will make a LOT more sense of what a class is :)

http://codewalkers.com/tutorials/54/1/html


Actual link is :
http://codewalkers.com/tutorials/54/1.html

--
Have you read the manual?
http://www.php.net/manual/en/

Jul 17 '05 #4

P: n/a
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 "description" 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 "description" 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["description"]
. " for just "
. " <b>" . $item["price"] . "</b><br>\n"
echo $buf;
}
printItem(mydata1);
printItem(mydata2);

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 'doSomethingWithMyItem' 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

Jul 17 '05 #5

P: n/a

"chris" <so*****@here.com> wrote in message
news:3f********@funnel.arach.net.au...
What is a class ?? is it like a function ??


A function is a collection of statements.
When a function is "called" by program code it can be fed data (the function
parameters) which the statements will process to "return" a result, the
function's return value.

A class is a collection of variables together with some functions which set
the values of those variables.
Classes are an integral part of "Object Oriented Programming".
The Basic of the '80s was NOT object oriented.

A class always has at least one function called its "constructor".
Calling this function results in the formation of one "instance" of the
class.
A class instance is known as an "object".

To work with an object you need to obtain a block of memory to store it in.
You do this by using the "new" operator.
This operator returns a pointer to the block of memory where the object will
be stored

e.g. in javascript there is a class called an array.
I would guess the array class consists of the constructor function plus a
dynamic array variable.
The constructor function, called Array(), returns an object which consists
of an array the contents of which are determined by the parameters fed to
the constructor.
Calling the constructor might look like this:
weekday=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

weekday is an object that consists of an array of the days of the week.

One book I have read likened an object to a cookie (real edible cookies -
not the kind you find on the web) and a class to a cookie cutter. The
definition of the class determines the shape of the object.

The PHP manual describes it thus:
"Classes are types, that is, they are blueprints for actual variables. You
have to create a variable of the desired type with the new operator. "

Check it out in the manual for yourself
www.php.net/oop
HTH
Orson


Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.