开发者

What is best practice on handling duplicate method calls?

Let's say I have a class loader I want to register.

$loader = new ClassLoa开发者_如何学Cder;

$loader->register();

When the method register() is called a second time, should I:

  1. Throw an exception stating that the class loader has already been registered
  2. Silently fail
  3. Return a boolean status

If I were to go with option #1, I would also provide a isRegistered() method offer the opportunity for some checking before it is called a second time.

What is the best way of handling this scenario?

Are there any other, better options?


Have you considered something like triggering a warning or a notice?

trigger_error("ClassLoader already registered", E_USER_WARNING);

Unlike exceptions, triggering errors is a good way to find flaws/disruptions in code without halting the execution of the program. You keep the backtrace and a get an entry in the error log. If a mistake like this (registering a class loader twice) goes on production, you'd want it to be transparent for the user (it's not critical), but with the programmer getting notified.


It depends what the method is doing. In this case, if you're loading a class to access static methods, I would silently ignore the request if the class has already been loaded.

In this case, the program will be left in the same state regardless of whether the class was loaded before (either it will load the class, or it will do nothing; the class will be loaded nonetheless).

If it's a class with instance methods/properties, then you won't want to fail; instead you'll want to create a new instance.

In this case, throwing an Exception when the class is already registered just creates a hassle for the developer using your code. However, if you do choose to throw an Exception, you have to provide an isRegistered() method for them to call before loading the class. I'd definitely go with #2 in this case though.


Here's my rule of thumb: If you're developing an application that others will build off of, triggering errors (or better yet, throwing exceptions) is an acceptable way of notifying fellow devs that they are unnecessarily re-registering your class.
However, if the code is just for you or a small group of people that are familiar with the code, triggering an error seems overzealous. Unless calling the register function is processor intensive, I'd say just returning false from a quick check at the beginning of the register function should be fine.


How about a Singleton, or a singleton like approach ? I use that for creating a global smarty object :

In config.php :

$GLOBALS['config']['SmartyObj']     = 0;

In your utils.php (or whatever you want to use) :

function smartyObject()
{
    if ($GLOBALS['config']['SmartyObj'] == 0)
    {
        $smarty = new SmartyGame();
        $GLOBALS['config']['SmartyObj'] = $smarty;
    }
    else
        $smarty = $GLOBALS['config']['SmartyObj']; 
    return $smarty;   
}  

The idea is that if the object exists, you return the same reference to that object. If not, you create it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜