Page 1 of 1
Do I gain anything by turning simple functions into classes?
Posted: Thu Jan 28, 2010 3:24 am
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;
}
}
}
Re: Do I gain anything by turning simple functions into classes?
Posted: Thu Jan 28, 2010 3:26 am
by Eran
No. There is no point in using classes as function containers.
Re: Do I gain anything by turning simple functions into classes?
Posted: Thu Jan 28, 2010 7:09 pm
by Sindarin
Could they help in organization, or is it bad practice to objectify everything?
Re: Do I gain anything by turning simple functions into classes?
Posted: Thu Jan 28, 2010 7:39 pm
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.
Re: Do I gain anything by turning simple functions into classes?
Posted: Thu Jan 28, 2010 7:58 pm
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.
Re: Do I gain anything by turning simple functions into classes?
Posted: Fri Mar 05, 2010 9:52 am
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;
}
Re: Do I gain anything by turning simple functions into classes?
Posted: Fri Mar 05, 2010 2:04 pm
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?
Re: Do I gain anything by turning simple functions into classes?
Posted: Mon Mar 08, 2010 8:33 am
by Sindarin
What's with the fascination with objects?
Let's say I get excited with every new thing I learn to use.
Sometimes I ask cause I am afraid if I am using it the wrong way.
Re: Do I gain anything by turning simple functions into classes?
Posted: Mon Mar 08, 2010 9:48 am
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
