473,799 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Classes vs functions

I've been using functions since I first started using php, but I've been
hearing more and more about classes, which I never really looked at that
much. I understand the whole OO programming aspect, but are there any
advantages to having classes as opposed to just functions? I've heard that
the classes are better because they only load the functions they need to,
where with an include file of functions, the whole page gets loaded, even
if you only use a couple functions on a certain page. I've searched around
and can't find any "official" mention of this though. So, is that true and
should I start migrating to using classes instead of functions?

--
VR
Jul 17 '05 #1
18 8740
vrillusions:
I've been using functions since I first started using php, but I've been
hearing more and more about classes, which I never really looked at that
much. I understand the whole OO programming aspect, but are there any
advantages to having classes as opposed to just functions? I've heard
that the classes are better because they only load the functions they need
to, where with an include file of functions, the whole page gets loaded,
even
if you only use a couple functions on a certain page. I've searched
around
and can't find any "official" mention of this though. So, is that true
and should I start migrating to using classes instead of functions?


No. That sounds rather confused to me. PHP obviously isn't omniscient and
thus has to parse all the source files it reads in.

André Næss
Jul 17 '05 #2
On 2003-12-11, vrillusions <vrillusions@no t_this.neo.rr.c om.invalid> wrote:
I've been using functions since I first started using php, but I've been
hearing more and more about classes, which I never really looked at that
much. I understand the whole OO programming aspect, but are there any
advantages to having classes as opposed to just functions?

If you do understand OOP, you should see the _possible_ advantages if used
right... (But if you see them, why did you choose for PHP anyway?
Doesn't J2EE seem more appropriate?)
I've heard that
the classes are better because they only load the functions they need to,
where with an include file of functions, the whole page gets loaded, even
if you only use a couple functions on a certain page. I've searched around
and can't find any "official" mention of this though. So, is that true and
should I start migrating to using classes instead of functions?


One could make a really big class with all functions in it. This would
be as dumb as not making groups of functions, in separate files.

--
verum ipsum factum
Jul 17 '05 #3
vrillusions wrote:
I've been using functions since I first started using php, but I've been
hearing more and more about classes, which I never really looked at that
much. I understand the whole OO programming aspect, but are there any
advantages to having classes as opposed to just functions?
I think so... But in PHP, you can probably do the same thing without OO.

What I personnaly like w/ OO (in general) is that it allows to have
different substitutable implementations of a same concept, with a
uniform representation.

Note that you clearly don't need classes to have ADTs in PHP, but this
won't give you polymorphism (or not as easily). Now whether you need
this or not depends on your application and coding style. Having mostly
used (more or less) OO languages since I began programming, I'm may be a
bit biased !-)

Another point that is more PHP-specific is that PHP not having any
module concept, classes can also be used to emulate modules, thus
uncluttering the global namespace.
I've heard that
the classes are better because they only load the functions they need to,
where with an include file of functions, the whole page gets loaded, even
if you only use a couple functions on a certain page. I've searched around
and can't find any "official" mention of this though. So, is that true
I've never heard of such a thing, and really doubt it's even possible in
PHP.
and
should I start migrating to using classes instead of functions?


That's another problem, and this one is up to you... But you probably
shouldn't care if you don't have a need for it.

My 2 <euro>cents,
Bruno

Jul 17 '05 #4
>(But if you see them, why did you choose for PHP anyway?
Doesn't J2EE seem more appropriate?)


Certainly not! Java's strong typing in combination with it's primitive
datatypes is a pain in the ass. So is it's obligation to either handle
exceptions or declare to be throwing them. It is allso a matter of
mentality: Java's developers tend to make everything unnecessary
complicated. For example this absurd idea of EJB (Enterprise Java Beans, a
part of J2EE) that everything is distributed. This means that looping
through a thousend objects and retrieving a value form each will require a
thousend remote method invocations (these are at least a hundred times
slower then local method calls, but if these objects happen to reside on the
other end of the world each method call takes half a second or more). (in
the end they had te reintroduce
local objects kind of through a back door).

The more generic (= reusable) your code gets, the more you
run into the inflexibilities and bad designs of Java. I'm not alone here,
look at the Lightwieght Languages workshop (http://ll1.ai.mit.edu/) and its
discussion archive
(http://www.ai.mit.edu/~gregs/ll1-dis...l/threads.html - takes
a few minutes to download)

But let's talk about the advantages of OO. It comes down to:
- delegation + polymorphism
- encapsulation
- inheritance

Delegation means that you want something done but you do not do it yourself
but have an object do it for you. This is done by using a service, wich is
dispatched tp a function on an object. Now what is the difference between
calling a normal function and using a service? Polymorphism! The class of
which the object is intstantiated is decisive for which function is actually
executed. For example if i am building an ecommerce website i could have a
class "DeliveryServic e" with a subclass "ExpessDelivery " and another
subclass "Standard Delivery". Somehwere in the order intake i ask the
customer to select a delivery service. On the basis of his selection i
instantiate the corresponding class and tell the object te delivery adress.
Then i can ask the object for the dilivery price and maybe how long the
delivery will take. If the object is an instance of ExpessDelivery it will
ececute the functions of ExpessDelivery to calculate the price, which will
probably be higher then for a StandardDeliver y.

So far you could reach the same result with some case swithches in normal
functions. But here comes the trick: When the owner of the ecommerce website
asks me to add another delivery service, let's say "Collect On Delivery",
where his customer can pay on delivery to the guy that delivers the order, i
can simply add another class CollectOnDelive ry and implement it's functions.
With case swithches i would have to add a case to each case switch. In order
to do that i would first have to find all these case switches hidden in
whatever function where i have (or worse, someone else has) once written
them. This makes well-designed OO software more flexible and easier to
maintain.

You can see it like this: in OOP we put our functions into a large
multilayered
case switch, called the class hierarchy, while in conventional
programming you have to replicate many case switches throughout you code.
So, if you have little case switches (including if-elseif-etc) you will
probably not
need OOP. If your case switches are becoming a headache you should obviously
have used OO right from the start.

OK, this is more then enough, i suggest someone else explains encapsualtion
and
inheritance and their advantages.

Greetings,

Henk Verhoeven,
www.metaclass.nl

Jul 17 '05 #5
With total disregard for any kind of safety measures vrillusions
<vrillusions@no t_this.neo.rr.c om.invalid> leapt forth and uttered:
I've been using functions since I first started using php, but
I've been hearing more and more about classes, which I never
really looked at that much. I understand the whole OO
programming aspect, but are there any advantages to having
classes as opposed to just functions? I've heard that the
classes are better because they only load the functions they
need to, where with an include file of functions, the whole page
gets loaded, even if you only use a couple functions on a
certain page. I've searched around and can't find any
"official" mention of this though. So, is that true and should
I start migrating to using classes instead of functions?


I usually point people towards this post at Sitepoint whenever
this question is raised: http://sitepointforums.com/showpost....8&postcount=14

Happy reading. :-)

--
There is no signature.....
Jul 17 '05 #6
On 2003-12-11, Henk Verhoeven <ne**@metaclass REMOVE-THIS.nl> wrote:
(But if you see them, why did you choose for PHP anyway?
Doesn't J2EE seem more appropriate?)


[long snipped on advantages of OOP]

I agree that OOP has it's advantages...

But my point was: If the OP really wanted to make use of these advantages,
why did he choose for PHP in the first place? (Assuming his project is
is not using OOP, as PHP never had really support for OOP).

--
verum ipsum factum
Jul 17 '05 #7
Tim Van Wassenhove wrote:
On 2003-12-11, Henk Verhoeven <ne**@metaclass REMOVE-THIS.nl> wrote:
(But if you see them, why did you choose for PHP anyway?
Doesn't J2EE seem more appropriate?)


[long snipped on advantages of OOP]

I agree that OOP has it's advantages...

But my point was: If the OP really wanted to make use of these advantages,
why did he choose for PHP in the first place?


<troll>
Because Java sucks ?
</troll>
(Assuming his project is
is not using OOP, as PHP never had really support for OOP).


Too big. Won't work.

Jul 17 '05 #8
Tim,
PHP never had really support for OOP


How do you mean? What OOP support do you miss in, let's say php4 ?

Greetings,

Henk Verhoeven.

Jul 17 '05 #9
On 2003-12-12, Henk Verhoeven <ne**@metaclass REMOVE-THIS.nl> wrote:
Tim,
PHP never had really support for OOP


How do you mean? What OOP support do you miss in, let's say php4 ?


A way to "hide" data in a class, never found a way to declare a
variable/method as private.

--
verum ipsum factum
Jul 17 '05 #10

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

Similar topics

3
3465
by: Logan K. | last post by:
What is a PHP class? I've seen a lot of talk about these and how they can help you out, but I've never seen a very good explanation. I've been working with PHP for just over 6 months and usually hard code everything into my scripts. Recently I've begun putting frequently-used things (like my MySQL connect function) in a seperate file and including them. But I still haven't seen the use of classes or even functions for that matter. And one...
1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
7
2274
by: Bora | last post by:
I usually find that a number of unrelated classes require the same kind of operations. However, I don't want to duplicate code in multiple places. In Java, I've seen those "Utility Classes", which are basically static (singleton) classes created to encapsulate related operations in one class. These methods can be called from other classes that need same functionaliy. Is there a similar concept in C++? I know that if two classes...
45
3628
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
3055
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to figure out what would be a "proper" OO design (in C++) for this. There may be alternatives to writing my own ODBC handle classes, and I may be interested in them, but I'd like to pursue this particular problem, if for no other reason than to...
12
3099
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already ordered. I'm just too curious about this one to wait for the book. I would like to know is if it's good php programming practice to use abstract classes instead of singleton classes. For exemple a login class. I've made one as an abstract class...
1
1393
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
4
1385
by: mharness | last post by:
Hello All, I've defined a number of classes that currently only include public variable declarations. I've been reluctant to add subroutines and functions for fear of taking a performance hit when I pass the class as an argument to another function and in a one case, store the class to a session variable. Right now I have all of my functions and subroutines in a single class that's becoming a bit unwieldy. What I'd like to do is move...
6
2944
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
0
9687
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
9541
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
10482
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
10251
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...
0
10027
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
9072
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
7564
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.