Discussion:
Static utility class?
Micky Hulse
2013-09-04 19:25:14 UTC
Permalink
Hi all!

Example code:

<https://gist.github.com/mhulse/6441525>

Goal:

I want to have a "utility" class that contain utility methods which should
have the option of being called multiple times on a page.

I think my main goal is to avoid having to "new" things ... I don't really
need to create an instance here (there's no need for a constructor in this
case).

Question:

Is the above simple pattern a good way to have a class that calls static
methods?

To put it another way, is there any reason why I would not want to use the
above code?

Basically I want to wrap these simple functions in a nice class wrapper and
have the ability to call them without having to jump through more hoops
than is necessary.

Are there any pitfalls to writing a class like this? Or, is this the
standard way of writing a simple "utility" class for this type of situation?

Please let me know if I need to clarify my questions.

Thanks for your time?
Stephen
2013-09-04 19:36:47 UTC
Permalink
Post by Micky Hulse
I want to have a "utility" class that contain utility methods which should
have the option of being called multiple times on a page.
This sounds simply like a library of functions that are implemented
using objects.

You can use the standard "require_once" in your various PHP source files
so you only deal with loading the library file in the first file in
which it is needed. It would not be loaded from other files.

Instantiate your static variables in the library file. Again, so that
only happens once.
--
Stephen
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Harkness
2013-09-04 20:15:05 UTC
Permalink
Post by Micky Hulse
I want to have a "utility" class that contain utility methods which should
have the option of being called multiple times on a page.
...
To put it another way, is there any reason why I would not want to use the
above code?
The main problem caused by static methods is testability. Mocking or
stubbing a static method requires using a PHP extension and ensuring that
the original is reset whether the test passes or fails. As long as your
utility methods don't perform actions you want to avoid during tests, this
is fine.

Good examples for a utility class are string-processing functions such as
parsing and formatting, trimming and/or normalizing empty string to null,
etc. You want tests to work against these methods directly since there's no
need to avoid the work.

You'll want to avoid static methods whenever you want to be able to fix the
behavior (stubbing) to test scenarios in the class under test. An example
is anything hitting a database or external service. In order to test that
your downloader sends the correct warning email to the sysadmin when a REST
call fails, you need to be able to force the REST call to fail. This is
easy to do when you plug in an instance implementing the API because you
can give it an implementation/mock that fails for all calls.

I can't say if what you're thinking of will make a good utility class since
the code you posted is fake. If you post some examples of methods you want
to make static, we can give you pointers on which are good candidates and
which are best left to instance methods.

Peace,
David
Micky Hulse
2013-09-04 21:09:36 UTC
Permalink
Thank you so much for the quick and very informative/educational
replies Stephen and David, I really appreciate it! :)
This sounds simply like a library of functions that are implemented using
objects.
Instantiate your static variables in the library file. Again, so that only
happens once.
"functions implemented using objects" sounds like exactly what I want.
Just out of curiosity, and sorry in advance for my ignorance, but does
the code I posted fit that type of pattern? If not, what would I need
to modify and how would I call it?

Actually, I should probably spend some time Googling before I ask you
for more details. :D

On Wed, Sep 4, 2013 at 1:15 PM, David Harkness
I can't say if what you're thinking of will make a good utility class since the code you posted is fake. If you post some examples of methods you want to make static, we can give you pointers on which are good candidates and which are best left to instance methods.
Sorry about the fake code. To be honest, I have not written the code
just yet ... I'm kinda wanting to find the perfect pattern before I
get too far down the rabbit hole (though, this is for some simple
utility functions, so refactoring things should be easy later on).

I'll be sure to post real code for any follow up questions.

For now, thanks to you guys, I have a ton to work with. Thanks again
for the pro advice!

Cheers,
M
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stephen
2013-09-05 00:16:24 UTC
Permalink
Post by Micky Hulse
Thank you so much for the quick and very informative/educational
replies Stephen and David, I really appreciate it! :)
This sounds simply like a library of functions that are implemented using
objects.
Instantiate your static variables in the library file. Again, so that only
happens once.
"functions implemented using objects" sounds like exactly what I want.
Just out of curiosity, and sorry in advance for my ignorance, but does
the code I posted fit that type of pattern? If not, what would I need
to modify and how would I call it?
Actually, I should probably spend some time Googling before I ask you
for more details. :D
Well, your code does not take advantage of the features of classes, but
the syntax is correct.

Global, static variables are not consistent with the design concepts of
OOP. But as a first step in moving from procedural code to OOP, which is
learning the syntax, go for it.
--
Stephen
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Micky Hulse
2013-09-05 00:37:10 UTC
Permalink
Thanks Stephen! I really appreciate the help! :)

In my PHP ventures over the years, I haven't made much use of static
variables/methods/properties ... I was thinking they might be useful
for this one bit of code, but based on your feedback (and David's) I
think I'll be heading down a different path.

Thanks again for the kick in the right direction!

Much appreciated!

Cheers,
Micky
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Rodrigo Santos
2013-09-05 00:55:46 UTC
Permalink
Hi, first, sorry for the bad English.

Yes, at least, as far as I know, this is the perfect way to do what you
want to do. Think like this: when you instanciate a class, you are
allocating memory. If you don't need any information stored, then you don't
need to allocate memory, right? So, it's is logic to have a class that you
will call only one fragment when you need it, rather than load the entire
class just for one method.

Just be careful to organize your utility methods by by meaning. Don't put a
method that make dating stuff and a method that write a random string in
the same class. By doing so, you are breaking the object orientation
purpose.
Post by Micky Hulse
Hi all!
<https://gist.github.com/mhulse/6441525>
I want to have a "utility" class that contain utility methods which should
have the option of being called multiple times on a page.
I think my main goal is to avoid having to "new" things ... I don't really
need to create an instance here (there's no need for a constructor in this
case).
Is the above simple pattern a good way to have a class that calls static
methods?
To put it another way, is there any reason why I would not want to use the
above code?
Basically I want to wrap these simple functions in a nice class wrapper and
have the ability to call them without having to jump through more hoops
than is necessary.
Are there any pitfalls to writing a class like this? Or, is this the
standard way of writing a simple "utility" class for this type of situation?
Please let me know if I need to clarify my questions.
Thanks for your time?
Micky Hulse
2013-09-05 01:06:38 UTC
Permalink
Hi Rodrigo, thanks for the help, I really appreciate it!

On Wed, Sep 4, 2013 at 5:55 PM, Rodrigo Santos
Post by Rodrigo Santos
Hi, first, sorry for the bad English.
Not bad at all! Very clear and well written reply (heck, it's better
than my native English writing), so thank you! :)
Post by Rodrigo Santos
Yes, at least, as far as I know, this is the perfect way to do what you want
to do. Think like this: when you instanciate a class, you are allocating
memory. If you don't need any information stored, then you don't need to
allocate memory, right? So, it's is logic to have a class that you will call
only one fragment when you need it, rather than load the entire class just
for one method.
Interesting! That makes a lot of sense.

Now you've piqued my interests! :)

I was going to head down a different path, but you've inspired me to
further explore the use of static methods/properties/variables/other.

A part of me just wants to learn more about PHP OOP, and using static
members is something I've not explored much. Seems like a simple
functional utility class would be a good time to play and learn more.
:D
Post by Rodrigo Santos
Just be careful to organize your utility methods by by meaning. Don't put a
method that make dating stuff and a method that write a random string in the
same class. By doing so, you are breaking the object orientation purpose.
Excellent tip! Thank you Rodrigo! I really appreciate the tips/advice
and inspiration. :)

Have a great afternoon!

Cheers,
Micky
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-09-05 06:07:46 UTC
Permalink
Post by Micky Hulse
Hi Rodrigo, thanks for the help, I really appreciate it!
On Wed, Sep 4, 2013 at 5:55 PM, Rodrigo Santos
Post by Rodrigo Santos
Hi, first, sorry for the bad English.
Not bad at all! Very clear and well written reply (heck, it's better
than my native English writing), so thank you! :)
Post by Rodrigo Santos
Yes, at least, as far as I know, this is the perfect way to do what you want
to do. Think like this: when you instanciate a class, you are allocating
memory. If you don't need any information stored, then you don't need to
allocate memory, right? So, it's is logic to have a class that you will call
only one fragment when you need it, rather than load the entire class just
for one method.
Interesting! That makes a lot of sense.
Now you've piqued my interests! :)
I was going to head down a different path, but you've inspired me to
further explore the use of static methods/properties/variables/other.
A part of me just wants to learn more about PHP OOP, and using static
members is something I've not explored much. Seems like a simple
functional utility class would be a good time to play and learn more.
:D
Post by Rodrigo Santos
Just be careful to organize your utility methods by by meaning. Don't put a
method that make dating stuff and a method that write a random string in the
same class. By doing so, you are breaking the object orientation purpose.
Excellent tip! Thank you Rodrigo! I really appreciate the tips/advice
and inspiration. :)
I'll second Rodrigo's opinion, but would like to comment that the name
of the class is misleading since it's called "Singleton". The singleton
pattern is used when you only ever want one instantiation of a class. In
your case you are using static methods, the object never needs to be
instantiated and so it doesn't fit this pattern. What you are creating
is far more consistent with the utility pattern.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Micky Hulse
2013-09-05 18:27:52 UTC
Permalink
I'll second Rodrigo's opinion, but would like to comment that the name of
the class is misleading since it's called "Singleton". The singleton pattern
is used when you only ever want one instantiation of a class. In your case
you are using static methods, the object never needs to be instantiated and
so it doesn't fit this pattern. What you are creating is far more consistent
with the utility pattern.
Ahhh, thanks so much for the clarification and details! That's very helpful.

I've updated my gist to reflect the pattern name:

<https://gist.github.com/mhulse/6441525>

Much appreciated. :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
2013-09-05 19:15:25 UTC
Permalink
Post by Micky Hulse
I'll second Rodrigo's opinion, but would like to comment that the name of
the class is misleading since it's called "Singleton". The singleton pattern
is used when you only ever want one instantiation of a class. In your case
you are using static methods, the object never needs to be instantiated and
so it doesn't fit this pattern. What you are creating is far more consistent
with the utility pattern.
Ahhh, thanks so much for the clarification and details! That's very helpful.
<https://gist.github.com/mhulse/6441525>
Much appreciated. :)
Probably sufficient (and easier for typing) to just call it Utility
since it follows the pattern but isn't the pattern itself :)

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Micky Hulse
2013-09-05 19:28:13 UTC
Permalink
Probably sufficient (and easier for typing) to just call it Utility since it
follows the pattern but isn't the pattern itself :)
Good call! Updated the example code.

Thanks again! I really appreciate the help. :)

Cheers,
Micky
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...