# Autofunc patch (automatically loading functions like autoload)

- Thread: https://externals.io/message/45829
- Original archive: https://news-web.php.net/php.internals/45829
- Started by Rack-Soft security on 2009-10-16T14:32:51+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #45829 — Rack-Soft security — 2009-10-16T14:32:51+00:00

URL: https://externals.io/message/45829#45829

Hello,

I am submitting quite an interesting patch to PHP 5.2.11 and PHP 5.3.0. It is used in production on hundreds of machines without any issues.

Basically the patch adds the autofunc function that performs quite in the same way like autofunc, only that it does it for functions. As do you know, if PHP can not find a function it will issue a fatal error. With autofunc you can find the function somewhere and load it. Management of large no OOP projects (or mixed OOP/procedural) is much easier with this. I know that there are workarounds for loading functions, like adding them static to classes, but this is not efficient and involves important changes to code.

I have attached some PHP files to test with, you must execute index.php that will load autofunc when the loaded functions can not be found.

Let me know if you have any questions regarding the patch. 

Best regards,
Bogdan

---

## #45830 — Samuel ROZE — 2009-10-16T15:09:52+00:00

URL: https://externals.io/message/45829#45830 · In reply to: #45829

Hi,

This is a good idea, it appears that it works fine ! I'm not really fond
of auto-loading (class or here functions) but it can be a good
thing ! :-)

Le vendredi 16 octobre 2009 à 17:32 +0300, Rack-Soft security a écrit :
[...]
> Management of large no OOP projects (or mixed OOP/procedural) is much
> easier with this.

Are there still new projects no OOP which are using PHP 5.x ?

> I have attached some PHP files to test with, you must execute
> index.php that will load autofunc when the loaded functions can not be
> found.

For people who are reading our message from archive, it must be better
the host these files on a web server. ;-)

For me, it is good. (the functionality, I didn't have the time to look
at the code)

Thanks for PHP.
Samuel.

---

## #45831 — Ferenc Kovacs — 2009-10-16T15:29:15+00:00

URL: https://externals.io/message/45829#45831 · In reply to: #45830

> Hi,
>
> This is a good idea, it appears that it works fine ! I'm not really fond
> of auto-loading (class or here functions) but it can be a good
> thing ! :-)
>
> Le vendredi 16 octobre 2009 à 17:32 +0300, Rack-Soft security a écrit :
> [...]
> > […]
>
> Are there still new projects no OOP which are using PHP 5.x ?
>
http://drupal.org/node/547518
"Why not use classes"
I didnt check the latest code, but I think drupal still uses global functions.
> > […]
>
> For people who are reading our message from archive, it must be better
> the host these files on a web server. ;-)
>
> For me, it is good. (the functionality, I didn't have the time to look
> at the code)
>
> Thanks for PHP.
> Samuel.

---

## #45835 — Larry Garfield — 2009-10-17T03:17:55+00:00

URL: https://externals.io/message/45829#45835 · In reply to: #45831

On Friday 16 October 2009 10:29:15 am Ferenc Kovacs wrote:
> 
> > […]
>
> http://drupal.org/node/547518
> "Why not use classes"
> I didnt check the latest code, but I think drupal still uses global
> functions.

That document is rather out of date and it's on my list to update soon, but 
yes Drupal is still largely procedural.  Even as we are adopting more OO code 
in version 7 (code freeze was yesterday!) and will undoubtedly use even more 
OO code in future versions, there are many use cases where functions or 
dynamically-called functions are simply a better solution in PHP than classes.

For that reason I am very very much in favor of this functionality, although I 
have not yet looked at the specific implementation here.  I'll try to do so 
soon, although I not really qualified to comment on the C code itself, just the 
API.

-- 
Larry Garfield
larry@garfieldtech.com

---

## #45838 — Larry Garfield — 2009-10-17T18:10:22+00:00

URL: https://externals.io/message/45829#45838 · In reply to: #45829

On Friday 16 October 2009 9:32:51 am Rack-Soft security wrote:
> Hello,
>
> I am submitting quite an interesting patch to PHP 5.2.11 and PHP 5.3.0. It
> is used in production on hundreds of machines without any issues.
>
> Basically the patch adds the autofunc function that performs quite in the
> same way like autofunc, only that it does it for functions. As do you know,
> if PHP can not find a function it will issue a fatal error. With autofunc
> you can find the function somewhere and load it. Management of large no OOP
> projects (or mixed OOP/procedural) is much easier with this. I know that
> there are workarounds for loading functions, like adding them static to
> classes, but this is not efficient and involves important changes to code.
>
> I have attached some PHP files to test with, you must execute index.php
> that will load autofunc when the loaded functions can not be found.
>
> Let me know if you have any questions regarding the patch.
>
> Best regards,
> Bogdan

Looking at the sample code in autofunctest.tar.gz:

- Why are you using __autofunc()?  As far as I'm aware the recommendation for 
classes these days is to never use __autoload() but `spl_autoload_register()`, 
since you can then stack autoloaders rather than running the risk of a 
duplicate function name collision.  I would recommend skipping the magic 
function entirely and going straight to a stackable approach a la 
spl_autoload*().  To wit:

func_autoload_register('myloadfunction');
func_autoload_unregister('myoldloadfunction');
func_autoload_functions();
func_autoload_call();

See: http://us2.php.net/manual/en/ref.spl.php

- I get the $VN_function global variable, as that's your lookup table.  
Naturally a production implementation would do something more robust, but it's 
good for example purposes here.  I do not, however, understand what's going on 
with $VN_global.  I just see an eval() and run in terror.  What's that all 
about?  Are you autoloading global variables too?  I don't understand.

- How would this handle namespaces?  Functions can have namespaces just like 
classes can, so autoloading functions should handle namespaces too, in much 
the same way as classes.

-- 
Larry Garfield
larry@garfieldtech.com

---

## #45839 — Stan Vassilev — 2009-10-18T05:02:12+00:00

URL: https://externals.io/message/45829#45839 · In reply to: #45829

Hi,

Unfortunately there may be a problem with this path and namespaces in 5.3. I tried to warn about this while namespaces were being implemented, but everyone was in the "autoload will always be for classes only".

Right now when you use fully qualified name of a class when inside a namespace, you need to always prefix it with backslash: \Foo. 

For functions, it was deemed too hard as most internal functions remain in global space, so inside a namespace one would need to prefix every call with a backslash. If you don't prefix with backslash, the interpreter will try both options at runtime: if there's a local function, it'll run, if there isn't, it'll try to find it in global space, and only if it's not there, it'll fail.

The only reason this double lookup works is because there's no autoload for functions, and it's the reasons why classes don't have this feature. There was a big discussion back about how to enable them both without severe performance penalty or unintuitive behavior, but none was found. 

I am myself a big proponent of autoload for functions, as there was never a good reason for excluding them from autoload (except for the artificially introduced one in 5.3).

Regards, 
Stan Vassilev 
  ----- Original Message ----- 
  From: Rack-Soft security 
  To: internals@lists.php.net 
  Sent: Friday, October 16, 2009 5:32 PM
  Subject: [PHP-DEV] Autofunc patch (automatically loading functions like autoload)


  Hello,

  I am submitting quite an interesting patch to PHP 5.2.11 and PHP 5.3.0. It is used in production on hundreds of machines without any issues.

  Basically the patch adds the autofunc function that performs quite in the same way like autofunc, only that it does it for functions. As do you know, if PHP can not find a function it will issue a fatal error. With autofunc you can find the function somewhere and load it. Management of large no OOP projects (or mixed OOP/procedural) is much easier with this. I know that there are workarounds for loading functions, like adding them static to classes, but this is not efficient and involves important changes to code.

  I have attached some PHP files to test with, you must execute index.php that will load autofunc when the loaded functions can not be found.

  Let me know if you have any questions regarding the patch. 

  Best regards,
  Bogdan
