473,804 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

maybe a stupid question regarding classes

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
5 1530
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
"chris" <so*****@here.c om> 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
"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
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

Jul 17 '05 #5

"chris" <so*****@here.c om> wrote in message
news:3f******** @funnel.arach.n et.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 "constructo r".
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","Mo n","Tue","Wed", "Thu","Fri","Sa t");

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1633
by: sebb | last post by:
I'm kind of newbie to programming, but I thought of something and I want some opinions on that. It's about a new instruction block to do some cycles. I thought about that because it's not very easy to program a cycle. Here is a simple example : b=0
7
1755
by: Martin Feuersteiner | last post by:
Hi! I would be grateful for any advise regarding what I'm doing wrong.. My brain is stuck. Probably some stupid simple mistake I don't see. Thanks very much for your efforts! Martin I have this code: DECLARE @ContactID varchar(10),
119
4643
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see, http://www.alistapart.com/articles/betterliving/ I read on http://msdn.microsoft.com/netframework/default.aspx?pull=/library/en-us/dnnetdep/html/netfxcompat.asp It said they changed stuff like this
2
1493
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public: virtual void Method(void)=0; };
16
2110
by: WindAndWaves | last post by:
Hi there I have $initstartdate = date("d-m-Y"); in my code How can I get it to be date() + 1 or 7 for that matter. Because my server is in the US and I am in New Zealand, they are always a day behind....
3
1912
by: mirko | last post by:
Hello, I have a problem with my include_path and I don't know why... Can anybody see the mistake? my configuration: PHP Version 4.3.11 System: Windows NT 5.0 build 2195
36
2864
by: Hoopster | last post by:
Hello, I know nothing about C++ but want to get started. Is there any good free C++ program that I can try to see if I like programming? I also need a good free compiler. I don't want to purchased the regular VB C++ until I know that I will like it.
9
2288
by: KarlM | last post by:
After reading some articles regarding confuguration data I'm a bit confused. Where is the right place for storing configuration data? - XML-files? - registry? - INI-files? (from a users point of view, ini-files are more comfortable to read and edit) Where should I store user specific config data? Where should I store machine specific config data?
6
1532
by: Malvolio | last post by:
I know this is a *really* stupid question but what the hell is visual c++? I downloaded it and it doesn't seem to work. It isn't like any c++ compiler I ever saw before and the manual on the Microsoft website did not improve my comprehension of it at all. How do you compile your source code with this thing? Is it a compiler at all? Am I crazy? I know it would probably take too much time to answer my dumb questions here but if you can...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.