473,507 Members | 3,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Advantages of using classes over functions?

Hi,

Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

Cheers

Burnsy

Jul 17 '05 #1
12 7389
On 2005-05-09, bi******@yahoo.co.uk <bi******@yahoo.co.uk> wrote:
Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?


What you're asking is why Object Oriented Programming (OOP) is better
than procedural programming. The answer is: That wholly depends on the
task at hand. Generally speaking OOP gives you some tools for managing
complexity in larger systems. It also provides a mean for eaiser reuse
of existing code. A good example of the power of OOP can be found here:

<http://www.sitepoint.com/forums/showthread.php?t=59898#post439958>

--
Cheers,
- Jacob Atzen
Jul 17 '05 #2
bi******@yahoo.co.uk wrote:
Hi,

Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?


This is a matter of opinion. One extreme states the fact that classes give
you nothing you can't have without them, and says why bother and does not
use them at all. The other extreme can't code "Hello, World!" without 5
layers of inheritance and a dozen composite objects.

So the real question is, what jobs are they good for? Also, what kind of
site are you building? Is there something you are having trouble doing?

In our system we write database applications, we use a dispatcher and
functions, plus a scheme for storing lots of config parms in
ready-to-execute include files that populate associative arrays. This
takes care of 99% of cases.

The only place where I have found it actually easier to use a class is for
providing overrides to default behavior. The one single class we have
contains some very basic behaviors, and contains calls at different points
to empty class methods. If I need to override default behavior, I make up
a class for that page (normally I don't need a class for most pages, the
libraries know what to do), and code up one or two of those trigger methods
to nudge processing in one direction or another.

But as for making one-class-per-table, yuck.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #3
Burnsy,
Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

Normally I code a massive amount of objects and simple functions and
arrays of configuration.

What I normally do is put all of my business logic into classes that
have actions. Then as the last poster noted that you can always extend
those operations which makes it extremely scalable.

Most things that I use for objects are authentication and also database.
This helps when I transfer to different DBs (have had to do it a
couple times, not pretty when things are just coded to one specific item).
Another nice thing about objects is it gives you a chance to group all
of your functions and have variables that can be global to all the
functions but not global within the scripts. That can help massively on
many cases. (template systems are huge examples of this type of
importance).

I'm sure there will be more stuff posted later but as stated before it
is a all about your opinion.

Mike
Jul 17 '05 #4
bi******@yahoo.co.uk wrote:
Hi,

Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

Cheers

Burnsy

This brings up a question I have (hope it's okay to divert the topic
slightly). Should I write a common piece of code as a callable function,
or should I just make it some script that I can include (seems like it
would save the function call). If I write a function, I have to include
the file where I keep the function, anyway. So I tend to simply include
the "function" inline and then document the include file with pseudo
inputs and outputs (what vars does it depend on and what vars does it
change).

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Jul 17 '05 #5
bi******@yahoo.co.uk wrote:
Hi,

Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

Cheers

Burnsy

As Jacob said, this all goes back to whether Object Oriented programming
is "better" than structured programming.

Basically - variables have states (hold information). Functions have
behavior (do things). Objects (the instantiation of classes in PHP)
have both.

Classes have an additional feature, though. They can abstract the data.
For instance - I create a lot of database classes. It doesn't take
long, but it has advantages. For instance - due to a change in
requirements on one site recently, I had to split a table. Basically, I
had the original table minus a little data. That missing data went into
a second table. A third table provided linkage between the other two.

There were > 200 pages dependent at least in part on this table. How
many needed to be changed? Zero. Everything was in a class; I modified
the class to handle three tables instead of one. I had to change two
administrative pages which were the actual cause of the change. But
everything else was OK.

Generally, writing OO code does take longer than non-OO code, just as
writing functions takes longer than doing something inline. The
advantage come when you reuse the code, or, as in above, need to make
changes to the back end.

OO is not the best in every circumstance. But it has its advantages.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #6
> Generally, writing OO code does take longer than non-OO code, just as
writing functions takes longer than doing something inline. The
advantage come when you reuse the code, or, as in above, need to make
changes to the back end.


Taking longer is a common misconception. It's the same thing with most
programming. The more you do it the less time it takes.

Also a class with structures I already have in place, I have a specific
way I write my objects. When I first started writing objects and trying
to find a way to make it as reusable as possible I was taking around 1
hr to write the class now that time is down to around 15 minutes.

Now when I look at that type of time I then look at how long it will
take me for a page that does not use my objects I have already written
and then just writing it all in one file and between using my reusable
code and creating one big file is a change from around 2 hours to 30
minutes.

The reason is that my reusable code has been tested many times, the
expected actions are tested and the limitations known. This way you do
not recreate the wheel.

But the same can be said for simple include files. I just like it
because of the difference in namespaces and I can use any variables
without cluttering the files and have to worry about using an existing
variable.

So I think that also needs to be looked at when using OOP. Is the
namespace :)

Mike
Jul 17 '05 #7
Following on from 's message. . .
So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

* Classes are good for when data needs to be packaged. This makes it
easier to use with sessions and do checks etc on it. So for example you
might have a user class which contains name, ID to start with plus
'embedded' functions for checking password etc. Stuff it in the session
and Bobs your uncle. Now add access permissions - (some data and some
manipulation functions) without having to touch the way the data gets
passes around behind the scenes in PHP. This also means it is ready to
run for your next project.

* If you have lots of components that get stuck together and contain
others then wrapping your data and operations up in classes means you
can deal with 'black-boxes'.

* Classes for classes sake are a pain.

* The trouble with OO is that you need to think before you code. There
is an art to this of course. IMHO you should look at the self-contained
elements for objectivising and small-scale common activities for
functionising.

* Reference documentation is possible but you need tools to do it. I
wrote my own.

* Finally you can put a huge amount of complexity into a single object
which is fantastic if you want to work with complex objects as building
blocks. Pre-fabrication. For example this is a screen with in-line
editing in a table, with standard layout features, navigation buttons,
and all database twiddles wrapped up inside somewhere. (Actually this
gets compiled into a database which a screen server picks out as
required so there is a whole lot of overhead but it means that if you
want a 100 screen system you can have it in no time flat.)

$SCREENOBJECT = new stdScreen('sutable','Suppliers','','menu01');
$SCREENOBJECT->AddBlock(new
buttonBlock(HAR,BUADD,'suedit?ID=0','Add','New supplier'));

$t=new tableElement('select SuID,SuName,SuTel,SuOurNote,SuAddress from
supplier order by SuName','No suppliers!');
$t->AddCol('Name','[SuName]');
$t->AddCol('Tel','[SuTel]');
$t->AddButton('Details',BUGO,'suedit?ID=[SuID]');
$t->AddEditArea('Address','','addr[SuID]','[SuAddress]',20,2);
$t->AddEditArea('Notes','','note[SuID]','[SuOurNote]',20,2);

$sb = new stdBlock('');
$sb->SetActiveElement($t);
$SCREENOBJECT->AddBlock($sb);
--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jul 17 '05 #8
Following on from 's message. . .
So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

* Classes are good for when data needs to be packaged. This makes it
easier to use with sessions and do checks etc on it. So for example you
might have a user class which contains name, ID to start with plus
'embedded' functions for checking password etc. Stuff it in the session
and Bobs your uncle. Now add access permissions - (some data and some
manipulation functions) without having to touch the way the data gets
passes around behind the scenes in PHP. This also means it is ready to
run for your next project.

* If you have lots of components that get stuck together and contain
others then wrapping your data and operations up in classes means you
can deal with 'black-boxes'.

* Classes for classes sake are a pain.

* The trouble with OO is that you need to think before you code. There
is an art to this of course. IMHO you should look at the self-contained
elements for objectivising and small-scale common activities for
functionising.

* Reference documentation is possible but you need tools to do it. I
wrote my own.

* Finally you can put a huge amount of complexity into a single object
which is fantastic if you want to work with complex objects as building
blocks. Pre-fabrication. For example this is a screen with in-line
editing in a table, with standard layout features, navigation buttons,
and all database twiddles wrapped up inside somewhere. (Actually this
gets compiled into a database which a screen server picks out as
required so there is a whole lot of overhead but it means that if you
want a 100 screen system you can have it in no time flat.)

$SCREENOBJECT = new stdScreen('sutable','Suppliers','','menu01');
$SCREENOBJECT->AddBlock(new
buttonBlock(HAR,BUADD,'suedit?ID=0','Add','New supplier'));

$t=new tableElement('select SuID,SuName,SuTel,SuOurNote,SuAddress from
supplier order by SuName','No suppliers!');
$t->AddCol('Name','[SuName]');
$t->AddCol('Tel','[SuTel]');
$t->AddButton('Details',BUGO,'suedit?ID=[SuID]');
$t->AddEditArea('Address','','addr[SuID]','[SuAddress]',20,2);
$t->AddEditArea('Notes','','note[SuID]','[SuOurNote]',20,2);

$sb = new stdBlock('');
$sb->SetActiveElement($t);
$SCREENOBJECT->AddBlock($sb);
--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jul 17 '05 #9
So far (oh, maybe 9 months of php) I havent figured out the benefit
either. For one thing, I dont like great big files full of functions.
I usually break peices of code and functions out to a separate file,
then include() it. When i want to reuse code, i just include
thatFunction.php at the top of the main script, then call the function
at the appropriate place. So, my scrpits end up having a bunch of
include()s at the top. To me, it makes it easier to trouble shoot a
particular function. I keep folders full of ThisOrThatType of
function.

j

Jul 17 '05 #10
So far (oh, maybe 9 months of php) I havent figured out the benefit
either. For one thing, I dont like great big files full of functions.
I usually break peices of code and functions out to a separate file,
then include() it. When i want to reuse code, i just include
thatFunction.php at the top of the main script, then call the function
at the appropriate place. So, my scrpits end up having a bunch of
include()s at the top. To me, it makes it easier to trouble shoot a
particular function. I keep folders full of ThisOrThatType of
function.

j

Jul 17 '05 #11

OOP may have some advantages for very large systems, but most of those
advantages belong to things that are not strictly OOP: strict variable
typing, type checking on all passed and returned variables, exception
handling.

My current code is nearly all OOP because OOP encourages certain
stylistic improvements that help me with error checking. But there is
nothing I do with OOP that you couldn't also do procedurally.

Jul 17 '05 #12
lkrub...@geocities.com wrote:
OOP may have some advantages for very large systems, but most of those advantages belong to things that are not strictly OOP: strict variable typing, type checking on all passed and returned variables, exception
handling.
You're neglecting the incredibly powerful concepts of inheritance,
polymorphism, abstraction and encapsulation. OOP isn't just traditional
procedural code with slightly different syntax, it's a whole different
way of breaking down problems and representing structures.

OOP has nothing to do with type-checking and exception handling.
My current code is nearly all OOP because OOP encourages certain
stylistic improvements that help me with error checking. But there is
nothing I do with OOP that you couldn't also do procedurally.


Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1367
by: Simon Harris | last post by:
Hi All, I'm new to asp.net (Migrating from 'Classic' ASP) I'm having troubles working out classes, functions etc... Current situation is this: index.aspx displays datalist with links to...
0
829
by: Stu | last post by:
Hi, Ive been using classes as output from webservices which until now have worked well and have serialised into the xml i wanted. Now i want to have a structure of clients which have accounts...
11
4109
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing...
0
860
by: The Confessor | last post by:
I have a rather sprawling application-in-progress in Visual Basic .NET 2003, and I'm using very specific naming conventions to avoid confusion and make the function of my code as self-evident as...
6
8779
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
13
1706
by: Simon Dean | last post by:
Hi, I have a couple of questions. If you don't mind. Sorry, I do get a bit wordy at times. First one just throws some thoughts around hoping for answers :-) 1) Anyone got a theory on the...
1
2435
by: /M | last post by:
Hi! As you know, one way to seperate platform dependent code from platform _independent_ code is to create an abstract base class which acts as an interface towards all platform independent code...
5
2798
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
2
1328
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello 1) I agree that useing classes is the best way to use VB.net. The question is how do you write a VB program without using classes? 2) For my own edification where can I see an actual...
3
1345
by: Ramu528 | last post by:
Hi friends, I am populating a drop box from a table jos_qualifications. can anybody tell me how to populate a drop box using classes. something like $db->setQuery( $query ); this is my code...
0
7221
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,...
0
7372
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...
1
7029
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...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
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,...
0
4702
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...
0
3190
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
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.