# differences in the way spl_autoload() works on windows and *nix with namespaces

- Thread: https://externals.io/message/45877
- Original archive: https://news-web.php.net/php.internals/45877
- Started by Herman Radtke on 2009-10-24T16:27:31+00:00
- Messages: 1
- 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

---

## #45877 — Herman Radtke — 2009-10-24T16:27:31+00:00

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

It appears that `spl_autoload()` has implicit namespace autoloading
support on windows due to the fact that the namespace operator is the
same as the directory separator on windows.  For example, the class
foo\Bar will evaluate to the path "foo\Bar.php" on windows.

I think `spl_autoload()` should also implicitly work on *nix to avoid
confusion and allow for portability.

I have started a patch below.  I can provide tests if this is agreeable.

Patch:
Index: ext/spl/php_spl.c
===================================================================
--- ext/spl/php_spl.c   (revision 289895)
+++ ext/spl/php_spl.c   (working copy)
@@ -229,8 +229,28 @@
        zval *result = NULL;
        int ret;

-       class_file_len = spprintf(&class_file, 0, "%s%s", lc_name,
file_extension);
+#ifdef PHP_WIN32
+       char *ns_name = estrndup(lc_name, class_name_len);
+#else
+       char *ns_name;
+       if (strchr(lc_name, '\\')) {
+               ns_name = estrndup(class_name, class_name_len);
+               char *c = ns_name;
+               while(*c) {
+                       if (*c == '\\') {
+                               *c = '/';
+                       }
+                       *c++;
+               }
+       } else {
+               ns_name = estrndup(lc_name, class_name_len);
+       }
+#endif

+       class_file_len = spprintf(&class_file, 0, "%s%s", ns_name,
file_extension);
+
+       efree(ns_name);
+
        ret = php_stream_open_for_zend_ex(class_file, &file_handle,
ENFORCE_SAFE_MODE|USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);

        if (ret == SUCCESS) {


-- 
Herman Radtke
hermanradtke@gmail.com | http://hermanradtke.com
