Do I gain anything by turning simple functions into classes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Do I gain anything by turning simple functions into classes?

Post by Sindarin »

Let's say we have this simple function,

Code: Select all

    
function stringIsAlphaNumeric($string)
    {
        if (strspn($string,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') == strlen($string))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
do I gain anything by turning it into a class and using this function in object context?

Code: Select all

    
class stringHandler{
 
public function __construct()
{
}
 
public function stringIsAlphaNumeric($string)
    {
        if (strspn($string,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') == strlen($string))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Do I gain anything by turning simple functions into classes?

Post by Eran »

No. There is no point in using classes as function containers.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Do I gain anything by turning simple functions into classes?

Post by Sindarin »

Could they help in organization, or is it bad practice to objectify everything?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Do I gain anything by turning simple functions into classes?

Post by Eran »

You asked if you'd gain anything. My opinion is that you won't. You might as well use namespaces or separate files to group functions if that's all you're after. Object oriented programming can definitely improve the structure of your code, but only if applied in a relevant manner, not blindly "objectifying" everything. You should read on basic concepts such as abstraction, polymorphism, inheritance, decoupling and the various design patterns that implement those concepts to real-world scenarios.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Do I gain anything by turning simple functions into classes?

Post by requinix »

pytrin wrote:My opinion is that you won't.
I agree, and would go so far as to say "converting" functions to classes would actually be harmful.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Do I gain anything by turning simple functions into classes?

Post by Sindarin »

Ah I was thinking that classes could be used like packages are used in Flash.

Are classes good to use if I want to create an error catching function?
Like,

function to upload a file
fileUpload(){}

function to get the last error code
fileUploadErrorCode(){}

or should I go plain like

if (!fileUpload())
{
return $error_code;
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Do I gain anything by turning simple functions into classes?

Post by requinix »

Classes should be used to represent objects. Nouns.

A User is an object.
A File is an object
A BlogPost is an object.
A File Upload Verifier is not an object - it is a verb.

However it does suggest something you could use an object for: a FileUploader.

What's with the fascination with objects?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Do I gain anything by turning simple functions into classes?

Post by Sindarin »

What's with the fascination with objects?
Let's say I get excited with every new thing I learn to use. :D
Sometimes I ask cause I am afraid if I am using it the wrong way.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Do I gain anything by turning simple functions into classes?

Post by Luke »

Classes can be a really useful tool, and it's good you are excited about them. Just use that excitement to learn to use them properly. Don't worry about using them improperly though. That's how you will learn. When you use classes improperly it quickly becomes obvious that something isn't right and then you can do something called "refactoring". Refactor enough times and you will have a nice little design :)
Post Reply