Jump to content

Classes VS Functions


SyLon

Recommended Posts

Wow, I am glad you asked this question because I am a little confused about the advantage of classes over functions as well. I have a little bit of understanding about classes, but why would I use them if I can just use functions?

The only reasons I could find is it makes your code neater and it cuts maintaining costs...
Link to comment
Share on other sites

Leon,
  I've been back and forth on this topic ever since php 4 was released, and I'm still more on the side of class-less "procedural code".

The biggest arguments that I've absorbed for using classes is portability. A "good" class should not try to do too much, it does what is does and and that's it, making it so that you can begin a brand new project and still be able to use that class and not worry about it failing.

Classes can also share functions. Say I have a database object and product object, and there's a method of product that I want to apply to my database widget. I shouldn't add a special function to the database class because I'm using that for every project that I own, but I don't have to. I don't even have to think about it though, I can use a product function whenever..
[code]$DBObject->Product::neatFunction();
// one object using another's function.[/code]

----------

Another idea.
You begin to wish you were using objects as soon as your functions begin to take four or more parameters. A lot of your most complicated functions could take one, the object, and work itself out from within. The object in that instance is just a big array.

------------

Final thought.
In some languages (from what I understand C++,Java, don't really know), classes and objects are a must in order to do anything. Adding Object oriented syntax to PHP is, in my opinion, most beneficial to those that are already used to being in that state of mind. It is argued as well, and I agree, that PHP's objects aren't really objects anyhow, as PHP is an interpretive language, so it's all kind of a rouse.


I'm only posting here to begin the discussion. I'm really asking the forum the same question, Leon. I'm not completely sold on PHP objects, and continue to write procedural code more often than not.
Link to comment
Share on other sites

The argument is more for Object Oriented code, vs Procedural Scripting code.

Objects encompass patterns of an application, and can be used in many applications, where as Procedural is (usually) tied to an individual circumstance, thus is not very portable at all.

Classes (and the objects derived from them) do a lot more than just act as a collection of functions.

This argument is as old as the computer age itself, thus I will leave it to you (and Google ;)) to make your own minds up.

It is also worth noting that both have their merits, and demerits.
Link to comment
Share on other sites

I've found that converting to an OOP style has made my code much faster to write and easier to maintain. It has really helped me to think in terms of objects. It was also a great way to get my SQL out of all the pages. So whenever I want to list a user's info, and say I add new info, such as letting them add their AIM screen name, now I only have to update one function and every page reflects it. Yes, you can easily do this with regular, non-class functions, but when writing, it's a lot easier to think "Oh, the user class has a getInfo() function, I use that here, instead of writing the sql here.

As for the speed - I entered the game contest here - I wrote my site in 2 days. Now, I'm still adding a few features, but I wrote a game that exceeded the critera in a few hours. Let's say 5. Partly because I already had a few classes I was able to port over quickly, and partly because just the way of thinking has made everything faster for me.
Link to comment
Share on other sites

In PHP itself, I don't know that there will ever be [b]complete[/b] Object (class) support, although great strides have been made in that direction in the last couple versions. I agree with the assessment that there are cases where functions serve you well, and there is no need for classes; however, this argument quickly dies when you get into larger, more complex applications where each piece of the application is needing to communicate with the other portions, yet keep its functionality all to itself.

bibby, your example of [i]sharing[/i] functions is not very good OOP (Object Oriented Programming) practice at all. In PHP4, it works fine, but in PHP5, if the classes are coded correctly, you would restrict access to member functions and only allow that class to act upon itself. Therefore, in the example you were citing, you would actually have to cause your product to call its own function:
[code]
<?php
// Why a DBObject would have a Product as an attribute, I'll never know
$DBObject->Product->NeatFunction();
?>
[/code]

Besides that, the organizational aspect of classes is a phenomenal addition to larger applications. Being able to define a User class, UserHandler and individual classes to house the attributes of your different user types (extending the base User class, of course) allows you to add additional levels of User accessibility without ever having to touch your template or your base code again. You can simply create additional user classes and return the right type of object based on the login itself.

All of these things are very in-depth, and without having at least an appreciation of OOP concepts, they may be a bit deep to jump right into, but I hope I've at least given some reasons for why objects may be the way to go.

[b]*EDIT*[/b]
jesirose just hit on an incredible part of OOP, too: reusability. If you have a class that is able to stand alone, you can then implement that code into any application with minimal difficulty, rather than having to rewrite things over and over again.
Link to comment
Share on other sites

Thanks guys!
You helped me a lot!
The problem with classes/oop in PHP is that you don't have to use it and it makes you think you don't need it.
In C# for example, I have to use Classes & OOP and they are much more useful there aswell.
Maybe it's because of the C# studio wich helps you with OOP and really makes things easier to write but I still don't see how Classes in PHP makes code faster to write and easier to maintain
Thanks, Leon.
Link to comment
Share on other sites

It's mostly relative to the size of the application, as mentioned before. A site with only one page, that is mostly static with perhaps a few dynamic properties, such as a blog, won't 100% justify the use of OO (though I still would, as do others) but a large site, that has many pages and lots of dynamic content, or interacts with many external sources, will 100% justify it's use.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.