Req #62162 [Opn->Sus]: Autoloading for namespaces

From: Date: Thu, 19 Nov 2020 17:19:28 +0000
Subject: Req #62162 [Opn->Sus]: Autoloading for namespaces
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230462@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62162&edit=1

 ID:                 62162
 Updated by:         cmb@php.net
 Reported by:        lcfsoft at gmail dot com
 Summary:            Autoloading for namespaces
-Status:             Open
+Status:             Suspended
 Type:               Feature/Change Request
 Package:            *General Issues
 PHP Version:        5.4.4RC1
 Block user comment: N
 Private report:     N

 New Comment:

This discussion actually demonstrates that (a) this bug tracker is
not suitable for that, and (b) discussion about this feature is
necessary.  So if you, or anybody else, is still interested in
this feature, please forward the request to the internals mailing
list.  For the time being, I'm suspending this ticket.


Previous Comments:
------------------------------------------------------------------------
[2013-06-04 05:00:47] m00n dot silv3r at gmail dot com

Autoloading namespaces would be a useful feature.

------------------------------------------------------------------------
[2013-01-28 13:23:18] shiranai7 at hotmail dot com

Hi. I am in no position to actually decide anything.

Using abstract classes was suggested by me as an alternative since abstract classes can contain both
functions and constants and can be autoloaded in current version of PHP without any changes. Also
there is virtually no performance penalty when calling a static method instead of plain function.

Nevertheless, I agree with your point - there is currently no native way to autoload namespaced
functions and constants without use of a class wrapper or the require_once() statement. While the
"all class way" is popular way to organise applications right now, this would be a nice
thing to have in PHP.

In earlier comment I mentioned possible solution involving one or more spl_*_register() functions.
While the first two (autoloading based on single function or constant) are not very pretty, the last
one could be the solution we are looking for.

I have thought this through and I might create a RFC page with detailed description of how it would
actually operate.

For now, consider this:

void spl_package_autoload_register (
  callable $autoload_function(namespace, type, name),
  [$throw = true],
  [$prepend = false]
)

bool spl_package_autoload_unregister (
  mixed $autoload_function
)

void spl_package_autoload_call(
  string $namespace,
  int $type, // SPL_PACKAGE_FUNCTION, SPL_PACKAGE_CONSTANT
  string $name
)

Note that the "package" prefix is used just for demonstration. It could be
"namespace" or something else, but I think that "package" is better name for
group of namespaced functions and constants.

------------------------------------------------------------------------
[2013-01-28 00:00:46] dac514 at hotmail dot com

Agreed with lcfsoft.

What's the point of namespacing if we are told to use static class methods 
instead?  To 
change underscores to backslashes because backslashes are "prettier" 
than underscores? That's called class encapsulation. We already had that 
feature.

Namespacing as a concept has been around for decades. For example, they were 
available in 
Perl in 1998. Namespaces as a concept is not inherently an OO feature. They are 
simply a 
container for a set of identifiers. Functions being valid identifiers in PHP.

Please reconsider.

Regards,

------------------------------------------------------------------------
[2012-09-05 09:03:59] judev at cuttlefish dot com

> But still - weird, mostly useless and overkill to implement just because 
someone does not like :: in his identifier.

I disagree that this is mostly useless - far from it. If we had autoloading based 
on namespace access then namespace-based package/bundle management becomes 
trivial.
Frameworks are having to implement this themselves at present, to take an example 
from Laravel we have to call:
Laravel\Bundle::start($bundle_name);
before we can access code in that bundle. Would be great if instead we could use 
the existing language features and just use a namespace to automatically load 
that bundles code.

------------------------------------------------------------------------
[2012-05-29 13:50:40] rpycka at gmail dot com

I would also like to see this feature, especially in the context of function autoloading and
procedural programming.

1) It would make PP in PHP way more pleasant & effective (no need to spam guards like
require_once, no need for indirect dispatchers, obviously, not loading code you don't need for
particular request).
For a language that grew on procedural programming paradigm it's a shame development in this
area has been stalled for so many years (afair nothing have changed since 4 in this regard).

2) There is a lot of code that could profit, e.g. Drupal.

3) Hacking with classes has its cons, eg. performance (what would be relative difference between
direct function call vs static method call and indirect (callbacks)?).

4) How hard can it be? Checking for registered callback and running the procedure before firing
error.

Proposed interface by shiranai7 at hotmail dot com looks ok.

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=62162


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


Thread (12 messages)

« previous php.bugs (#230462) next »