PDA

View Full Version : Classes vs. Functions


Socraties
04-25-2003, 09:13 AM
I thought I would spark an interesting thread here.

Is there really a big difference in writing PHP code with classes versus functions. I know with other languages that I have used the OO methology has been useful, I just don't see a particular reason to use classes in PHP.

The answer may be simply a matter of preference, but what I am looking for is a more definate answer to why writing PHP pages using classes would be better than just straight functions.

If anyone can answer this that would be great, examples of some code using classes instead of functions would be cool to see as well.

The reason I ask is I have yet to see a reason for using PHP in an OO type fashion.

Thanks for any responses.

missing-score
04-25-2003, 01:32 PM
I prefer using classes for larger programs, becuase it is object based. The example I have attached is for mysql, so there would be no need for more than one object, but here is an example...

A simple class/function:


class addNum {

var $num = 5;

function addToNum($numb)
{
$this->num = $numb; // Sets the $num to the function argument
return ($this->num+$numb);
}

}


Now, I need to initialize:

$class = new addNum();

$num is sort of global within the $class variable, So i can use this:


echo $class->addToNum(4); // Would print 9, and set the $num var to 4. Also, you cannot print class vars outside, so:

echo $num; // Would echo nothing.


Now it seems like this could be done with a function, which it could. But with a class, You can have more than one object:

$class2 = new addNum();

Class2 can now be accessed in the same way as the original, but the internal class vars do not run in to eachother. With a function, you would have to make outside variables global. Allowing the use only once.

I hope you understand this. I have included my sql class, which I use for databases. It is big, but it might give you an idea. If you want to see the original functions version, just ask.

mordred
04-25-2003, 04:24 PM
Originally posted by Socraties
Is there really a big difference in writing PHP code with classes versus functions. I know with other languages that I have used the OO methology has been useful, I just don't see a particular reason to use classes in PHP.


Just curious, what reasons did you have to use OOP in other languages? Sometimes, like in Java, you have no other choice because everything (or most of it) in a particular language is basically object-oriented, but in the other cases you surely had another reason to use it?

I ask this because most of the same reasons can be applied for PHP programming too. At least that's what I do. Let's look at the general reason to use OOP. It's not (or it should not be ;)) because you jump on the bandwagon, but you want

* maintainability
* extendability
* code reuse

to be better in your application. OOP should support reaching these goals better than procedural or structured programming. Some key features of OOP I find very valuable for creating robust frameworks, like

* encapsulation
* inheritance
* polymorphism

plus, IMO, it's lot easier to document OO code with the proper tools. Let's have a simple example: You have a variable that shall store the state of an action performed, and that action is changed by a functionality called "makeSomething". Without OO, you would write sth. like:


$state = 0;

function makeSomething($param) {
global $state;

// do sth.


$state = 'myResult';
}


That code is produced by programmer A. Now programmer B writes a different part of the application that also makes use of a global $state variable. What happens? Both functions rely on the same variable and that creates confusion. To remedy this situation, you'd have to the inner code of one of these functions. But if you've written different classes, like


class Foo {
var $_state;

function makeSomething($param) {
// execute sth.
$this->setState('myResult');
}

// other methods etc.
}


and the same for class Bar, both modules can save the state independently without the chance of accidentally influencing the other. Just a quick example, I hope you get the idea. If not, ask. :)

There are also PHP extensions and frameworks (like PEAR, Eclipse, Horde, BinaryCloud) which are all object-oriented. If you want to use one of these components, you have no choice but to use it with OOP code. So refraining from OOP programming is cutting you off from a great deal of good code, which would'nt make sense to me.

It surely depends on your usage of PHP. If your goal is just to hack together a crude guestbook, don't take the overhead of learing OOP to do that. Same for quick hacks and rapid prototypes. If you develop a framework for large CMS or groupware application, think twice about.

That's all from me sofar... let's discuss. ;)

missing-score
04-25-2003, 04:26 PM
Good example mordred. You are better at explaining things than me :D

Socraties
04-25-2003, 06:51 PM
Wow!! great responses :D. I have used OOP for those same exact reasons for maintainabilty and portability. I have yet to build a large extensive app in PHP.

Thanks for the responses they are great.

boywonder
04-26-2003, 01:28 AM
This past winter I put together a fairly large (at least for me) project in PHP, by far my largest to date. The client was in a rush. I really needed to get it up and running fast. I used functions for everything and they suited my purposes, but I still wound up with spaghetti code and a fairly unorganized system and I knew it. I promised myself when the dust settled I would go back and re-write everything into a nice structure and make it a learning experience, which is where I'm at now.

So I've been doing much reading on OOP and OOP for PHP in particular. I am currently reworking the application to use classes, one section at a time. The difference is really unbelievable. Once you start viewing everything as an object the landscape changes dramatically. I guess you could say I have "seen the light". :) Prior to this I was unimpressed with PHP classes, like many people, thinking that "my functions are just as good". I was wrong.

Not claiming that everyone should use classes, but it sure works for me. Maintainability is my biggest concern.

OOP getting a lot more attention for the next version of PHP too... some interesting reading here: http://www.zend.com/zend/future.php