php-general Digest 4 Sep 2013 22:25:25 -0000 Issue 8356

Topics (messages 322034 through 322038):

Static utility class?
        322034 by: Micky Hulse
        322035 by: Stephen
        322036 by: David Harkness
        322037 by: Micky Hulse

message to user after complete POST
        322038 by: iccsi

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
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?

--- End Message ---
--- Begin Message ---
On 13-09-04 03:25 PM, Micky Hulse wrote:
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


--- End Message ---
--- Begin Message ---
On Wed, Sep 4, 2013 at 12:25 PM, Micky Hulse <mickyhulse.li...@gmail.com>wrote:

> 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

--- End Message ---
--- Begin Message ---
Thank you so much for the quick and very informative/educational
replies Stephen and David, I really appreciate it! :)

On Wed, Sep 4, 2013 at 12:36 PM, Stephen <stephe...@rogers.com> wrote:
> 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
<davi...@highgearmedia.com> wrote:
> 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

--- End Message ---
--- Begin Message ---
I have a POST form and action itself like following
<form name="MyForm"  id="MyForm" method="POST" action="index.php">
</form>

I want to show success message when POST complete or error message if there is any. I would like to know are there any property or global variable I can check to show message to users. If not, it seems that the only solution is jQuery, since it has success function that jQuery call after POST.

Your help and information is great appreciated,


Regards,

Iccsi,


--- End Message ---

Reply via email to