|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-02-08 11:27 UTC] landeholm at gmail dot com
Description:
------------
I'm building a framework which has a custom gettext implementation since the gettext extension of PHP is poorly designed for my needs and it's not installed in all environments so relying on it reduces compatibility. The problem however is that it's installed in some environments and since it claims the global "_" function which is pretty much a gettext standard alias (and other gettext functions), it's preventing me from implementing the gettext standard. There are no possible way for me to solve this problem nicely today because PHP does not have the ability to either:
A. Override/undeclare/rename native functions. (Okay, I can do it via APD but that makes extension dependability even WORSE.)
B. Unloading extensions at runtime. (Most preferable... I don't want it at all)
C. Importing functions. (My framework uses namespaces. The fact that functions cannot be imported by the "use" keyword really spoils this feature. Otherwise it could actually have fix this problem.)
Note that this problem assumes a context where you can't control the environment in which you install your application in. This is a very real scenario for a lot of people including me. This is a practical problem, not a theoretical one.
Also note that declaring the "_" function in a namespace would be pointless:
1. it would no longer be compatible with the gettext standard
2. it would require refactoring of all existing string wrapped code
3. it would no longer be compatible with existing string wrapped code
4. a longer name like \foo\translate_lib\_() defeats the point of having a short function name
Another workaround is to declare a _ forwarding function in every possible namespace, but that solution is dumb and ugly.
As a temporary workaround I might declare something like t\s() but I don't like that solution and it doesn't solve 1, 2 and 3 above.
Test script:
---------------
/** EITHER A: */
undeclare_function("_");
/** OR B: */
unload_extension("gettext");
/** OR C: */
namespace foo;
use foo\translate_lib;
/** Test: */
// My gettext implementation.
function _($msgid) {
return translate($msgid);
}
echo _("hello");
Expected result:
----------------
bonjour
Actual result:
--------------
Fatal error: Cannot redeclare _()
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 11 05:00:02 2025 UTC |
Some workaround notes: I'm currently solving this by the workaround previously described as "dumb and ugly" by a little twist. I'm dynamically adding forwarders for the functions that needs to be imported by using a routine that goes trough all namespace locations and creates a forwarder function by evaluating generated PHP code that looks something like: namespace source_ns; function source_fn() { return call_user_func_array('target_ns\target_fn', get_func_args()) } ... Eval is slow though... If I could populate the symbol table directly instead doing it by eval() this would be acceptable. What I want is dynamic function declaration like declare_function($name, function() { ... }); The eval method however works temporary since the low amount of function imports only makes this routine use a couple of ms.