Re: Static methods vs. plain functions
| From: | Sebastian Krebs | Date: | Thu, 19 Sep 2013 16:44:51 +0000 |
| Subject: | Re: Static methods vs. plain functions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322105@lists.php.net to get a copy of this message | ||
2013/9/19 Simon Dániel <simondani91@gmail.com>
> 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!
> Not to mention that a function is
> more efficient than a class method.
>
Actually I wouldn't be so sure about that.
>
> 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