Function vs Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Which do you prefer...

Functions
19
32%
Classes
40
68%
 
Total votes: 59

Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Function vs Class

Post by Gen-ik »

I just wanted to know from PHP programmers if you prefer to use functions or classes and why?

Personally I use functions but this is many due to the fact that I have not found any good reason to use classes yet.

Your thoughts and views on this would be great :D
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well anyone who uses classes will say both, so I voted for classes just because I think that's more what you meant.

But I beleive we've beat this topic to death in previous threads....
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

this could start another OO vs Not war :)

In general, Pure functions are faster and if you do not plan to use OO for anything else than grouping functions its not much point..

If you do take advantage of OO stuff it can be a prettier structure and easier to reuse and layer things in the future, PHP5 is supposedly a lot better at OO when it comes to speed..
ik
Forum Commoner
Posts: 34
Joined: Thu Jul 10, 2003 5:33 am
Location: Lancs Uni, UK
Contact:

Post by ik »

Well, my 2 cents...

My native langauge :roll: is C++, so... classes.
PHP classes don't give too much, but two things are of importance:
1. separated namespace, preventing from global mess
2. limited but useful inheritance.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Classes - OO provides the extra-level of abstraction that allows me to see design-patterns anyone would struggle with if they only used functions. (yes, I have started reading the book of four ;) )

But as nielsene said, in PHP4.+ it's not an either or question. Let's see where PHP5 will take us.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

When I first started I couldn't grasp the need of classes. Now I couldn't live without them. Classes interacting with each other is a heaven sent!
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

JPlush76 wrote:When I first started I couldn't grasp the need of classes. Now I couldn't live without them. Classes interacting with each other is a heaven sent!
i was the same way... didn't really understand what I was missing until I got into them a little bit. I was about a third of the way through writing a complete shopping cart system some years ago when I scrapped everything and rewrote it using OO techniques. smartest decision i ever made.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

i still use functions, because i really can't understand the way using classes... am i a DUMB?
:oops: :oops: :oops:
can someone really teach me?
:?: :?: :?:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

mudkicker wrote:i still use functions, because i really can't understand the way using classes... am i a DUMB?
:oops: :oops: :oops:
can someone really teach me?
:?: :?: :?:
There are links to tutorials in this sticky. Also, have a look at the tutorial section of http://www.zend.com, they also have one about object oriented programming.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

nielsene wrote:Well anyone who uses classes will say both, so I voted for classes just because I think that's more what you meant.

But I beleive we've beat this topic to death in previous threads....
I'd say "methods and classes" or functions ;)

star flaming here
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Well, if there was still an option to vote, I would vote for functions. I feel that in a web environment, execution time takes precedence. I feel comfortable saying this becuase I know code reuse and a good clean design can be had without the use of objects.

On the other hand, I do think objects are awesome. But I've decided that I'll save the objects for compiled languages.

Cheers,
BDKR
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

I choose classes, because I like OO Programing :D No I was just joking. I use classes, because they give me a great oportunity to code closed modules which works like an... hmm... object :D, each by itself. I don't have to think if that function is needed here or not, in class everything is needed. Other great thing is that, when someone write class for me, it's easier to know what the hell is going out there ;). Functions are great where it's up to some small counter, but in big application OO is a must.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

delorian wrote:I choose classes, because I like OO Programing :D No I was just joking. I use classes, because they give me a great oportunity to code closed modules which works like an... hmm... object :D, each by itself. I don't have to think if that function is needed here or not, in class everything is needed. Other great thing is that, when someone write class for me, it's easier to know what the hell is going out there ;). Functions are great where it's up to some small counter, but in big application OO is a must.
i agree with you. totally!!! :P
lastcraft
Forum Commoner
Posts: 80
Joined: Sat Jul 12, 2003 10:31 pm
Location: London

Post by lastcraft »

Hi...
BDKR wrote:I feel that in a web environment, execution time takes precedence. I feel comfortable saying this becuase I know code reuse and a good clean design can be had without the use of objects.
On the small scale maybe, but I feel this is a little short term. You can get some reuse from functions, but you are always limited. With functions data is either global or you have to pass it around. Global data is the enemy of reuse, and yet passing and marshalling of data incurs an overhead. As applications get larger these two factors squeeze terribly.

OO brings the data and the functions closer together.

In functional languages you can pass functions around as easily as any data parameter, but PHP is not a functional language to this level (array_map excepted). If you want to move operations around they have to be bundled into objects in PHP. As the number of functions in your code goes up, the average distance that you have to move your data goes up in proportion. Any small efficiency you get from the simpler method dispatch is soon drowned by the extra method calls. With OO you pass less data.

Also "code-smithing" gives a very poor return on investment. If you need performance do you really un-OO your code? Hardly. More likely you introduce caching, use a faster data storage solution, white a C module or use lazy loading. These types of optimisations give order of magnitude improvements. Twiddling your code will give a few percent in all but exceptional cases.

So how easy is it to cache, lazy load, import a module or change database in a script/function based environment? I don't even intend to find out. From smaller scale projects alone I know that I can achieve it far faster with an OO code base.

An example? We were recently parsing a large number of web pages for a metacrawler and things were running a little slow (by about a factor of 100!). Our optimisations were...

1) Run the fetching with first a C, then a Perl solution with multiple processes as a separate module. It simply saves the results batch as a file. This gave us a factor of 20+.
2) Adding our own DNS server with it's own cache. This gave us 25% for very little extra effort.
3) Placing the temporary downloaded files into a RAM disk. Gave us a factor of two with nearly zero effort.
4) Caching the parsed search results in MySQL. This is the most work, but gives us a factor of 3+. DBM would give us more, but see below.
5) Now we were ready to profile we found that due to a bug, multiple hits were generating spurious method calls with empty results. A simple fix gave us 50% greater performance.
6) We found the next() call in the file iterators was the bottleneck because the fread() method takes too long. We created an iterator that loaded the file with a single next() with only about two dozen lines of code to write and one to change (we just had to drop in the new iterator). This gave us a factor of 2 with less than an hour's work.
7) Placing the main reusable cached data into MySQL allows us to access the cache over a network, which in turns mean there is no real state in the server. This allows us to have multiple servers load balanced sharing the same cache. We currently have three of these.

Doing more than about one of these refactorings in a functional version would have pushed us into a complete rewrite (with the exception of the DNS). With OO the impact was minimal and quick and allowed us to experiment and reprofile with ease.

What would you save with code smithing some thing this large? How much would you slow development? We never needed to hand optimise our code because we got a factor of 1300 times improvement by changing the design. We could do that easily because it was OO.

yours, Marcus.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

You now Marcus, that was a rather simple black and white response.

Anwyays, I hear your points, but let's be honest here and state that many of the issues you've brought up concerning maintenance of non-oo code is worst case scenario. As a matter of fact, I've seen some serious nightmares in OO.

I'm presently digesting the QIII engine, which is written in C FYI, and it's not tough to extend or change. Not to the point you make it out to be.

Or is there something else I don't know here? Hmmm? The Linux kernel and Windows OS are both rather modular (however flawed). They're both written in C too. Windows even uses objects, as much as the C language will allow that is.

This isn't as clean as you portray it too be. You've just raised the same old battle cry that other dedicated OOP'rs out there do, and you've cried it out in the same old simplistic fashion.

I think OO is cool, but even more so, Object Oriented Design and Responsibility Driven Design. Niether of which requires an OO language, pure or hybrid.

Clean, modular, easy to maintain code bases existed before the wide spread use of OO. OO just makes it easier to attain a clean, modular, easy to maintian code base.

Cheers,
BDKR
Post Reply