Discussion:
Static methods vs. plain functions
Simon Dániel
2013-09-19 16:28:32 UTC
Permalink
Hi,

I am working on an OOP project, and cannot decide which way to follow when
I have to write a simple function.

For example, I want to write a function which generates a random string. In
an OOP environtment, it is a matter of course to create a static class and
a static method for that. But why? Isn't it more elegant, if I implement
such a simple thing as a plain function? Not to mention that a function is
more efficient than a class method.

So, in object-oriented programming, what is the best practice to implement
such a simple function?
Sebastian Krebs
2013-09-19 16:44:51 UTC
Permalink
Post by Simon Dániel
Hi,
I am working on an OOP project, and cannot decide which way to follow when
I have to write a simple function.
For example, I want to write a function which generates a random string. In
an OOP environtment, it is a matter of course to create a static class and
a static method for that. But why? Isn't it more elegant, if I implement
such a simple thing as a plain function?
I'd say: Definitely!
Post by Simon Dániel
Not to mention that a function is
more efficient than a class method.
Actually I wouldn't be so sure about that.
Post by Simon Dániel
So, in object-oriented programming, what is the best practice to implement
such a simple function?
In "strict"-OOP [1] you would choose a static method, because functions are
simply forbidden. However, PHP isn't strict about that by itself. So I for
myself don't like the dogmatic "We use classes and nothing else!"-approach.
If a function fits better, it's OK to be a function.

[1] Actually that would end up in a mix of OOP and "class-oriented
programming", which isn't that strict.
--
github.com/KingCrunch
Aziz Saleh
2013-09-19 16:55:20 UTC
Permalink
I think that it would be more elegant if you are already in a OOP to keep
the flow and stick to OOP. It just doesn't make sense to me in an
environment that uses OOP to have functions laying around.

Personally I like to group similar functionality together in their own
objects, this way I can reuse them on different projects, the random string
generator is an excellent example of something I usually use in almost all
of my projects.

Function calling is usually faster than object calling (depends on how you
benchmark it) since there is an overhead to it. There are some who tried to
"benchmark" this and had opposite results, It all comes down to how are you
going to use that functionality:

http://www.webhostingtalk.com/showthread.php?t=538076
http://www.micro-optimization.com/global-function-vs-static-method

Personally in my projects - specifically if I use a framework, I try to
stay away from making standalone functions unless absolutely necessary.
Post by Sebastian Krebs
Post by Simon Dániel
Hi,
I am working on an OOP project, and cannot decide which way to follow
when
Post by Simon Dániel
I have to write a simple function.
For example, I want to write a function which generates a random string.
In
Post by Simon Dániel
an OOP environtment, it is a matter of course to create a static class
and
Post by Simon Dániel
a static method for that. But why? Isn't it more elegant, if I implement
such a simple thing as a plain function?
I'd say: Definitely!
Post by Simon Dániel
Not to mention that a function is
more efficient than a class method.
Actually I wouldn't be so sure about that.
Post by Simon Dániel
So, in object-oriented programming, what is the best practice to
implement
Post by Simon Dániel
such a simple function?
In "strict"-OOP [1] you would choose a static method, because functions are
simply forbidden. However, PHP isn't strict about that by itself. So I for
myself don't like the dogmatic "We use classes and nothing else!"-approach.
If a function fits better, it's OK to be a function.
[1] Actually that would end up in a mix of OOP and "class-oriented
programming", which isn't that strict.
--
github.com/KingCrunch
Sebastian Krebs
2013-09-19 17:55:27 UTC
Permalink
Post by Aziz Saleh
I think that it would be more elegant if you are already in a OOP to keep
the flow and stick to OOP. It just doesn't make sense to me in an
environment that uses OOP to have functions laying around.
buzzword: multi-paradigm. Thats why it could make sense ;)
Post by Aziz Saleh
Personally I like to group similar functionality together in their own
objects,
- That aren't objects, but classes. Actually you don't programm in
object-oriented, but in class-oriented (or probably a mix)
- You can (imo "should") use namespaces
Post by Aziz Saleh
this way I can reuse them on different projects, the random string
generator is an excellent example of something I usually use in almost all
of my projects.
Function calling is usually faster than object calling (depends on how you
benchmark it) since there is an overhead to it. There are some who tried to
"benchmark" this and had opposite results, It all comes down to how are you
http://www.webhostingtalk.com/showthread.php?t=538076
http://www.micro-optimization.com/global-function-vs-static-method
Personally in my projects - specifically if I use a framework, I try to
stay away from making standalone functions unless absolutely necessary.
Post by Simon Dániel
Post by Simon Dániel
Hi,
I am working on an OOP project, and cannot decide which way to follow
when
Post by Simon Dániel
I have to write a simple function.
For example, I want to write a function which generates a random
string. In
Post by Simon Dániel
an OOP environtment, it is a matter of course to create a static class
and
Post by Simon Dániel
a static method for that. But why? Isn't it more elegant, if I implement
such a simple thing as a plain function?
I'd say: Definitely!
Post by Simon Dániel
Not to mention that a function is
more efficient than a class method.
Actually I wouldn't be so sure about that.
Post by Simon Dániel
So, in object-oriented programming, what is the best practice to
implement
Post by Simon Dániel
such a simple function?
In "strict"-OOP [1] you would choose a static method, because functions are
simply forbidden. However, PHP isn't strict about that by itself. So I for
myself don't like the dogmatic "We use classes and nothing
else!"-approach.
If a function fits better, it's OK to be a function.
[1] Actually that would end up in a mix of OOP and "class-oriented
programming", which isn't that strict.
--
github.com/KingCrunch
--
github.com/KingCrunch
Paul M Foster
2013-09-19 17:11:16 UTC
Permalink
Post by Simon Dániel
Hi,
I am working on an OOP project, and cannot decide which way to follow when
I have to write a simple function.
For example, I want to write a function which generates a random string. In
an OOP environtment, it is a matter of course to create a static class and
a static method for that. But why? Isn't it more elegant, if I implement
such a simple thing as a plain function? Not to mention that a function is
more efficient than a class method.
So, in object-oriented programming, what is the best practice to implement
such a simple function?
"Best practices" are for academics and people who read Datamation.

You have to look at why OOP exists and then ask yourself if the function
you wish to create really needs any of the values that attend OOP. You
also have to look at how simple your code is to read and understand.
Based on what you've described, there would appear to be absolutely no
value in making it into a class with a static method. In that case, all
you would have done is to add an extra level of complexity to your code.
If I were a programmer coming in after you to work with your code, I'd
ask myself why in the world you did that. And if feasible, I would
change it back to a flat function for the sake of simplicity.

Always prefer non-OOP unless you have some compelling reason to make
something object-oriented. For example, the interface to a DBMS is
something which may involve many many functions. It is definitely
something which benefits from OOP code, not flat functions. I've
personally found that dates benefit from this same treatment.

Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...