473,661 Members | 2,453 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 7408
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.c om/forums/showthread.php? t=59898#post439 958>

--
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*******@attgl obal.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('suta ble','Suppliers ','','menu01');
$SCREENOBJECT->AddBlock(new
buttonBlock(HAR ,BUADD,'suedit? ID=0','Add','Ne w supplier'));

$t=new tableElement('s elect SuID,SuName,SuT el,SuOurNote,Su Address from
supplier order by SuName','No suppliers!');
$t->AddCol('Name', '[SuName]');
$t->AddCol('Tel' ,'[SuTel]');
$t->AddButton('Det ails',BUGO,'sue dit?ID=[SuID]');
$t->AddEditArea('A ddress','','add r[SuID]','[SuAddress]',20,2);
$t->AddEditArea('N otes','','note[SuID]','[SuOurNote]',20,2);

$sb = new stdBlock('');
$sb->SetActiveEleme nt($t);
$SCREENOBJECT->AddBlock($sb );
--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.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('suta ble','Suppliers ','','menu01');
$SCREENOBJECT->AddBlock(new
buttonBlock(HAR ,BUADD,'suedit? ID=0','Add','Ne w supplier'));

$t=new tableElement('s elect SuID,SuName,SuT el,SuOurNote,Su Address from
supplier order by SuName','No suppliers!');
$t->AddCol('Name', '[SuName]');
$t->AddCol('Tel' ,'[SuTel]');
$t->AddButton('Det ails',BUGO,'sue dit?ID=[SuID]');
$t->AddEditArea('A ddress','','add r[SuID]','[SuAddress]',20,2);
$t->AddEditArea('N otes','','note[SuID]','[SuOurNote]',20,2);

$sb = new stdBlock('');
$sb->SetActiveEleme nt($t);
$SCREENOBJECT->AddBlock($sb );
--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.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.ph p 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

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

Similar topics

1
1383
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 sites - If no link found in DB, I want to display a message instead. I worked out I need a function to do this
0
834
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 which have securities So ive defined the following classes (See below) when I look a the xml when it gets to the <clientsaccounts> i dont see the full serialised definition
11
4125
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 the actual structure itself. Is this true of C# also?
0
868
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 possible. Below is an example, the name of a recently-declared Command Button: TabControl_Main_TabPage_MapEditor_TabControl_TileProperties_TabPage_Graph ic_Button_AddGraphic I'd love it if I could, in the interests of clarity, refer to that...
6
8792
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
13
1724
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 usage of PHP Classes rather than an actual technical guide? I've seen loads of things that show how to put together a class, but without actually necessarily saying why you'd want to use a class over say a separate file of functions or explaining:
1
2443
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 and implement platform specific code in the sub classes. Since the platform (in most cases I quess :) ) can't vary during the execution of a program, using virtual functions to specify the interface seams a bit odd. I'm thinking about using...
5
2807
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 myself, but I have a feeling I won't get far. Please help me complete the exercise. I am posting what needs to be done and will be updating with pieces of code that I come up with. Thank you all for your attention. ...
2
1338
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 "full blown" VB .net program that doesn't use classes?
3
1356
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 which i have written without using classes ::::::::::::: $result = mysql_query( "SELECT category FROM jos_qualifications" ) or die("SELECT Error: ".mysql_error()); // start the select list echo '<select name="category"...
0
8343
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
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
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...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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
4179
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...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
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.