Edit report at https://bugs.php.net/bug.php?id=62162&edit=1

 ID:                 62162
 Comment by:         shiranai7 at hotmail dot com
 Reported by:        lcfsoft at gmail dot com
 Summary:            Autoloading for namespaces
 Status:             Open
 Type:               Feature/Change Request
 Package:            *General Issues
 PHP Version:        5.4.4RC1
 Block user comment: N
 Private report:     N

 New Comment:

lcfsoft at gmail dot com,
You cannot expect anything to be loaded just by an "use" statement. It just 
defines a local alias for a class or namespace. The classes get loaded only 
when they are actually used.

Based on your example I think that you are looking for a way to "autoload 
functions". While it could possibly be handy in your case, I dont think it will 
ever be an actual feature. It is not even possible to import a function or 
constant through the "use" statement, so why this?

Using an abstract class is not "hacking around" in this case. Why do you need 
to do this:

dispatching\dispatch(...);

if you can do this?

dispatching::dispatch(...);

The functionality is IDENTICAL, with added bonus of possible autoloading. Do 
you really need that \ instead of :: ? That is the only actual difference.


Previous Comments:
------------------------------------------------------------------------
[2012-05-28 15:31:07] lcfsoft at gmail dot com

shiranai7 at hotmail dot com,
why would I use a class if I don't need a class?

Anyway, ways to hack around are well know. The idea here is that we wouldn't 
need 
to "hack around". Since we have namespaces - it can be done.

------------------------------------------------------------------------
[2012-05-28 14:44:46] shiranai7 at hotmail dot com

I cannot imagine any "valid" use case for this. Autoloading is designated for 
classes only (that may happen to be  located in a namespace). If you need 
autoloading functionality for group of functions, put them as static methods  
inside an abstract class.

Example:
--------

namespace MyFramework\MVC;

abstract class Dispatching
{
    static public function myFunc() { ... }
    // etc
}

--------

Then you can take advantage of the autoloading.

------------------------------------------------------------------------
[2012-05-25 19:34:28] lcfsoft at gmail dot com

Description:
------------
While OOP is conquering the world, "a function" is still sometimes enough.

Introduce functionality for autoloading namespaces (of grouped functions, 
classes 
etc), in the same manner that exists for autoloading instantiated classes.

(there is a similar feature request here https://bugs.php.net/bug.php?
id=52385&edit=2, the key difference is that while autoloading for functions 
could 
get complicated, - for namespaces it is as straightforward as for classes)

Test script:
---------------
function __autoload($namespace)
{
    require $namespace. '.php';
}

//require_once 'myframework/mvc/dispatching.php'; // - want to get rid of these
use myframework\mvc\dispatching; // - here it gets autoloaded even though it's 
not a class.

dispatching\dispatch(...);



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62162&edit=1

Reply via email to