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

McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

malcolmboston wrote:ive never used classes, but have downloaded many, which unfortunately ive never used i do think that they are bloated just by looking at the size of the script

i have started to use functions, and cant praise them enough :wink:
Like everything, there's good code and bad. Take a look at http://www.students.cs.uu.nl/people/voo ... /index.php.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

lol, i downloaded that yesterday!!!!

unfortunately, what i find with 99% of classes is that they add way way too much so called 'functionality' in to them which simply isnt needed most of the time

like eclipse for eg, some of the classes i have absolutely no idea what they do, maybe its just me i dont know :?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Eclipse is lean, mean and an excellent example of OOP design which is well worth taking some time to study.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

*noted*

ok i have a few questions maybe theyve been answered a thousand times before

whats the key difference(s) between classes and functions (classes = OOP, functions not?)

why should i spend my time learning classes?

hope you can answer them buddy

mal
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

The link provided by markl999 (a post by the author of Eclipse), and one of Lastcraft's posts above explain the advantages of classes better than I can.

Really it depends on your aims. If you just want to hack together simple web sites it doesn't matter how you code - php lets you hack away.

If you plan to work as a programmer building web apps in php or you just want to learn about programming in general, you need to know OOP.
Last edited by McGruff on Wed Mar 24, 2004 1:36 pm, edited 2 times in total.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

thank you McGruff

helpful as always
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

just a few things ive noticed
link wrote: The biggest advantage of OOP is re-use. Re-use comes in three flavors (mostly), but only one is used a lot, while the second and third are almost never used. PEAR, for example, uses only two flavors (and the second flavor minimally).
so are functions? :?
for eg the function in the code [url=viewtopic.php?t=19863]here] is being reused 5-10 times with just passing different variables to it, am i missing something here?
link wrote: The second form of re-use in OOP is class inheritance: given some class, you 'extend' it, adding new features or overriding existing ones. Inheritance comes in many forms, and I'll briefly describe three of the forms here, comparing each of them to procedular programming.
now that sends like fun and very cool
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Using a class allows you to set certain perameters when starting the class, that you can't exactly carry over into other functions when using functions.

IE: Instead of doing function1($userid, $var1); ... function2($userid, $vars); ... function3($userid, $var3); ...you would do: $user = new User($userid); ... $user->function2($var2); ... $user->function3($var3);

...if you understand what I'm trying to say.
Image Image
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

phice wrote: ...if you understand what I'm trying to say
lol, no i dont, you are obviously more clued up than i am, im sure ill learn it hell i couldnt understand functions 24 hours ago, now ive written half a dozen

maybe i willl start to look more in depth with classes shortly but at the moment they scare me lol, the syntax confuses me to a certain point, but then again so did sql_queries at first so i guess you just live and learn.......

well thanks guys
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

malcolmboston wrote:lol, i downloaded that yesterday!!!!

unfortunately, what i find with 99% of classes is that they add way way too much so called 'functionality' in to them which simply isnt needed most of the time

like eclipse for eg, some of the classes i have absolutely no idea what they do, maybe its just me i dont know :?
Eclipse can be great, or can be a mix/mash of things you don't need.

For example, I generally use adodb for my database needs - I love the abstraction, the robust commandset support, the session handling, and more.

However, it is definitely overkill for most projects. Eclipse fits nicely in the niche for smaller projects trying to use less code to do simple tasks.

As McGruff said, it is very tightly written (lean).. however, I'd say thats true of the modules IN eclipse.. Eclipse has numerous functions (sybase, ms-sql, mysql, etc) for multiple types of databases. To me, that means a number of classes that arent being used .. not lean.

However, the same is true of adodb.

So, like I said, its nice for some things, and not for others. I do agree with McGruff on one point - its very well written, and well designed. I just think they could have chosen other ways to deal with some issues (abstract DB to a single class, with seperate members, depending on the DB).
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Roja wrote: Eclipse has numerous functions (sybase, ms-sql, mysql, etc) for multiple types of databases. To me, that means a number of classes that arent being used .. not lean.
Not exactly: only one set of db classes will actually be loaded in an app's scripts. And don't necessarily have to install them all if you want to save a few k disk space.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

im definitely going to have a look at eclipse tonight, its just first impressions of it confused me so i steered clear
hopefully now i have a fairer understanding of functions i can learn classes easier
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

classes for me. anything else is just another number.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

McGruff wrote:Jason was talking about procedural v OOP code not speed.

Lastcraft nailed the often-stated (but mistaken) speed issue, I think.
My mistake. I thought Jason was pointing out that using classes does not neccessary mean your creating objects. My bad. But this does not change my feeling that using classes does not help me to write tight, fast code in PHP. Please forgive my heresy for not using classes. If it helps you any I do think of solving problems in terms of objects. :wink:

:twisted:
Post Reply