I implemented this feature in a project of mine (called Sonata), this
is very easy in 1.1 to override the way helpers are loaded by
overriding the "loadHelpers" method of applicationConfiguration (which
use_helper() is a simple wrapper).
loadHelpers:
try {
.. old way ..
} catch (InvalidArgumentException $e) {
.. new way ..
}
My "new way" of loading a helper is :
- find the "<name>Helper" class, which must override "sntHelperBase"
- generates (if needed [*]) the "<name>.cache.php" file in the cache,
that simply contains "shortcuts" [**] for the public non-static [***]
methods of the class, in a comprehensive way : all parameters are in
the functions profiles, type-hinting, call by reference, and php-doc
is copied too (thanks Reflection classes !).
- include the generated file ;) and that's all folks !
Imagine such a helper :
class MyUrlHelper extends sntHelperBase
{
function linkTo($text, $url) {
...
}
}
We can use it exactly as the old way :
use_helper('MyUrl');
echo link_to('home', '@homepage');
Which is equivalent to :
$helper = new MyUrlHelper();
echo $helper->link_to('home', '@homepage');
The main advantages :
> - ability to use object helpers
Yes :) Of cours this is the main goal.
> - ability to extend existing helpers
Yes or no, the "old-fashioned" helpers are not extendable :(
But all "new way" helpers are fully extendable : just have
"MyNewUrlHelper extends MyUrlHelper" and "use_helper('MyNewUrl')" does
the trick :D
> - ability to override existing helpers
Same as the ability to extend.
> - ability to use function helpers
Yes, required for BC and I always think using underscored functions
for helpers is a good idea as view should not use anything else than
simple helpers, it allows to detect it easily.
> - BC
Full.
> - performance (through cache)
Absolutely required, I can't imagine the cost of regenerating the
shortcuts-file each time.
[*] When cache is regenerated ?
Cache is regenerated if it has expired, or if it is older than the
file implementing the helper class.
[**] What are shortcuts ?
The generated file contains a __<className>_instance() function that
implements a singleton.
And a shortcut is simply a function (undescored) that includes php-
doc, exact same parameters, and call the method of the singleton.
e.g.:
function link_to($text, $url) {
return __MyUrlHelper_instance()->linkTo($text, $url)
}
[***] Why non-static methods ?
Because static inheritance is really crappy in PHP ^^
On 4 juil, 15:41, Michael Nolan <[EMAIL PROTECTED]> wrote:
> On Jul 4, 1:09 pm, "Francois Zaninotto" <[EMAIL PROTECTED]
>
> project.com> wrote:
> > As for the compatibility layer, due to the poor ability of PHP to
> > define functions dynamically, couldn't we imagine a code generator
> > that creates helper files in the cache with functionhelperscalling
> > their object counterpart? Something like the admin generator
> > mechanism...
>
> I think someone would have to code it as well as imagine it... unless
> you've written a thought-to-PHP converter :-D
>
>
>
> > That way, we would have:
>
> > - ability to use objecthelpers
> > - ability to extend existinghelpers
> > - ability to override existinghelpers
> > - ability to use functionhelpers
> > - BC
> > - performance (through cache)
>
> > What do you think ?
>
> It's quite magic, but as long as it's reliable I like it because of
> the flexibility it allows.
>
> Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---