Re: How expensive are function_exists() calls?

From: Date: Wed, 04 Mar 2009 12:01:29 +0000
Subject: Re: How expensive are function_exists() calls?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-289224@lists.php.net to get a copy of this message
..not an internals question me thinks ... redirecting to generals mailing list mike schreef: > I am trying to figure out a strategy for multiple output formats on a > site, and it seems like I can have functions defined by default, but > have them defined -after- I've included the targetted format first. > > However that would require > > file1.php: > > function foo() {} > > file2.php > > if(!function_exists('foo')) { > function foo() {} > } > > Is this a very expensive thing to do? There would be a good number of > functions that would be leveraging this. Is it a no-no? or is it > pretty cheap? it busts opcode caches. so I wouldn't recommend it at all, don't do conditional includes or function/class definitions if you can help it. idea: use some kind of registry pattern to register outputters, the the code requiring an outputter can request it from the register. 1. I would think about using a class for each outputter (instead of a function) ... chances are you will have need for more than just 'foo' 2. register all the default outputters 3. register some optional, overriding outputters dependent on the context. <?php abstract class OutputRegistry { private $objs; static function register($key, $obj) { self::$objs[$key] = $obj; } static function get($key) { return self::$objs[$key]; } } // default OutputRegistry::register('renderer', new HTMLRenderer); // overriding (in some other file) to allow creating JSON output OutputRegistry::register('renderer', new JSONRenderer); > > Thanks > - mike >

« previous php.general (#289224) next »