Re: [PHP-DEV] How expensive are function_exists() calls?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



..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
> 


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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux