# PHP 5.3 Autoload+Namespaces+Functions incorret (atleast wierd) behaviour.

- Thread: https://externals.io/message/35862
- Original archive: https://news-web.php.net/php.internals/35862
- Started by thrthr@gmail.com on 2008-02-29T15:02:27+00:00
- Messages: 4
- 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

---

## #35862 — thrthr@gmail.com — 2008-02-29T15:02:27+00:00

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

So I've been messing around with the latest 5.3-dev builds on my
localhost, after testing out the Namespace-feature in 5.3-dev by
porting parts of my own framework to it I ran into some buggy (or
atleast wierd) behaviour concerning autoload and functions inside
namespaces.

Basicly, the call is this: Demo::Functions::test(); where both Demo
and Functions are namespaces, and test() is a function inside the
Demo::Functions namespace. If you have an autoload handler here and
the file containing the Demo::Function::test()-decleration isn't
loaded, autoload will be called.

Now everything seems fine, but the problem is that when the autoload
hooks have all fired php looks for the class Demo::Function, because
autoload seems to be interpreting the Demo::Function::test(); call as:
<Namespace>::<Class>::<Function>, when it should be
<Namespace>::<Namespace>::<Function>, and since the Function class
doesn't exist it will trigger a fatal error.

I have included an example in the __autoload.zip
(http://arenagrande.tv/__autoload.zip), containing 3 files, check the
comment in demo.php for a better explenation.

However, I can see why php would behave this way, since autoload was
intended for classes and not functions, but imho. autoload should be
made to work perfectly with namespaces aswell as classes.

---

## #35912 — johannes@php.net — 2008-03-03T11:40:52+00:00

URL: https://externals.io/message/35862#35912 · In reply to: #35862

Hi Fredrik,

On Fri, 2008-02-29 at 16:02 +0100,
"=?ISO-8859-1?Q?Fredrik_Holmstr=F6m?=" wrote:
> Basicly, the call is this: Demo::Functions::test(); where both Demo
> and Functions are namespaces, and test() is a function inside the
> Demo::Functions namespace. If you have an autoload handler here and
> the file containing the Demo::Function::test()-decleration isn't
> loaded, autoload will be called.
> 
> Now everything seems fine, but the problem is that when the autoload
> hooks have all fired php looks for the class Demo::Function, because
> autoload seems to be interpreting the Demo::Function::test(); call as:
> <Namespace>::<Class>::<Function>, when it should be
> <Namespace>::<Namespace>::<Function>, and since the Function class
> doesn't exist it will trigger a fatal error.

autoload was designed for loading classes not functions. Wen resolving
Demo::Function::test() and no function having that name is being found
it's assumed that Demo is the namespace, function a class name in there
and test a static method call. There's no way for the engine to see what
else might be meant. So the result is expected and changing it woud mean
to do major changes wich just creates new problems.

johannes

---

## #35913 — Marcus Boerger — 2008-03-03T12:27:55+00:00

URL: https://externals.io/message/35862#35913 · In reply to: #35912

Hello Johannes,

Monday, March 3, 2008, 12:40:52 PM, you wrote:

> Hi Fredrik,

> On Fri, 2008-02-29 at 16:02 +0100,
> "=?ISO-8859-1?Q?Fredrik_Holmstr=F6m?=" wrote:
> > […]

> autoload was designed for loading classes not functions. Wen resolving
> Demo::Function::test() and no function having that name is being found
> it's assumed that Demo is the namespace, function a class name in there
> and test a static method call. There's no way for the engine to see what
> else might be meant. So the result is expected and changing it woud mean
> to do major changes wich just creates new problems.

Actually we can (I think). There are to possibilities:

new namespace::class()
new namaespace::namedspace::class()

and so on, or:

namespace::class::functoin()
namespace::function()

and so on. That is, when detecting braces after it and a new in front
than it is a class. If there is no new in front it must be a function and
autoload needs to be called for all but the last part. With new it needs
to be called for all parts. Since the compiler already knows whether it
generates a ctor call we must have an issue that should be fixable.


Best regards,
 Marcus

---

## #35914 — johannes@php.net — 2008-03-03T12:55:53+00:00

URL: https://externals.io/message/35862#35914 · In reply to: #35913

Hi Marcus,


> new namespace::class()
> new namaespace::namedspace::class()
> 
> and so on, or:
> 
> namespace::class::functoin()
> namespace::function()
> 
> and so on. That is, when detecting braces after it and a new in front
> than it is a class. If there is no new in front it must be a function and
> autoload needs to be called for all but the last part. With new it needs
> to be called for all parts. Since the compiler already knows whether it
> generates a ctor call we must have an issue that should be fixable.

new in front is easy but take you're last example:

ns::func()

How can we detect, that we don't look for a class called "ns" with a
(static) method "func" but for a namespace "ns" with a regular function
called "func"? We do the lookup for "ns::func" in the function table,
then a class lookup for "ns" and then trigger autoload...

So the declaration for the thing called above could either look like

<?php
namespace ns;
function func() {}
?>

or like

<?php
class ns {
    static function func() {}
}
?>

And since we do only autoloading of classes, not functions, the autoload
is only triggered looking for the second case.

Every approach which might be cleverer would, imo, need some list of
known namespaces which the current design doesn't have at all.

johannes
