|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-09-22 16:01 UTC] jo at feuersee dot de
Description: ------------ This is basically the same as PHP bug #48129. Yes, I have read it "won't fix" My opinion on this is "won't fix" is not an option because it _is_ a bug and not fixing bugs does not work: 1) It is common practice in OO languages (including PHP) to give classes case sensitive names. Even the classes of PHP itself are case sensitive and usually start with capital letters (eg. DateTime, Exception, ...). PHP related projects like PEAR, Zend Framework etc. do the same. 2) In order to get a proper 1:1 mapping from class name to the file containing the PHP class definition, projects like PEAR or Zend Framework use the case sensitive class name, eg. System.php contains the class System. Again, this is common practice in other OO languages like C++. 3) What happens when the file system is case sensitive? See example: the script fails because the PEAR class System will be looked for in a file named system.php which does not exist because it is called System.php The workaround is using SPL_autoload_suxx instead. But look at the code: there are several compatibility issues (include_path separator : vs. ;), it does work but is not at all convenient. 4) What would happen if spl_autoload() wouldn't lowercase the class name when looking for a class definition? a) Filesystem is case sensitive It would work! The spl_autoload() would look for a file called System.php which exists, thus will be require'd b) Filesystem is not case sensitive It would still work! The spl_autoload() would look for a file called System.php Because the file system is case insensitive, it would use either System.php or system.php (or sYSTEM.PHP - you got the point?). Because on case insentive filesystems both files "System.php" and "system.php" are not allowed in the same directory, there is _no_ issue with backward compatibility. The only circumstances where it would break backwards compatibility would be on filesystem which is case insensitive but does not allow capital letters. Any real live examples of such a file system? Conclusion: The current specification of spl_autoload() with implicit lowercasing is excactly wrong. There has been, is and never will be any gain in this 'feature' since the class name itself inside PHP is case sensitive. Reproduce code: --------------- <?php /** * Demonstration of the current incompatibility * Make sure you have PEAR inside your PHP include_path */ // this should work but doesn't spl_autoload_register('spl_autoload'); // this does work //spl_autoload_register('SPL_autoload_suxx'); /** * Does the same as spl_autoload, but without lowercasing */ function SPL_autoload_suxx($name) { $rc = FALSE; $exts = explode(',', spl_autoload_extensions()); $sep = (substr(PHP_OS, 0, 3) == 'Win') ? ';' : ':'; $paths = explode($sep, ini_get('include_path')); foreach($paths as $path) { foreach($exts as $ext) { $file = $path . DIRECTORY_SEPARATOR . $name . $ext; if(is_readable($file)) { require_once $file; $rc = $file; break; } } } return $rc; } $binaries = array( 'mysql' => System::which('mysql'), 'mysqlbinlog' => System::which('mysqlbinlog'), 'php' => System::which('php') ); print_r($binaries); ?> Expected result: ---------------- Array ( [mysql] => /usr/bin/mysql [mysqlbinlog] => /usr/bin/mysqlbinlog [php] => /usr/local/bin/php ) Actual result: -------------- PHP Fatal error: Class 'System' not found in /srv/www/vhosts/www.easy-sew.de/ftpjung/bin/autoload.php on line 38 Patchesphp_sql_autoload_mixed_case_fix (last revision 2012-06-15 20:00 UTC by jdornan at stanford dot edu)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 08:00:01 2025 UTC |
>The reason here is that is spl_autoload becomes case >sensitive, it will break scripts which depend on spl_autoload being >case insensitive. spl_autoload() was introduced in PHP 5.1.2 which is case sensitive concerning class names. This implies that if an operation on an unknown class is done, spl_autoload() is triggered and executed with the case sensitive name of the class. Thus we have 4 different possibilities: 1) The class name all lower case, the file containing the class definition is all lower case (eg. $foo = system::bar(); system.php) This will work independent wether spl_autoload() is lowercasing or not, since all is lowercased. Note that if the class defined in the file system.php is actually named System it wouldn't have ever worked because the class system is still not defined, which would trigger an error. 2) The class name all lower case, the file containing the class definition is uppercased (eg. $foo = system::bar(); System.php) This wouldn't work anymore on file systems which are case sensitive if spl_autoload() would skip lowercasing. Note that this would only have worked if the file system is case insensitive and the class definition in System.php would define a class "system". 3) The class name contains upper case letters, the file containing the class definition is lowercased (eg. $foo = System::bar(); system.php) This is what currently isn't working at all but would work at least for case insensitive file systems if lowercasing would be dropped. Note that if the class defined in the file system.php is actually named system it wouldn't have ever worked because the class System is still not defined. 4) The class name contains upper case letters, the file containing the class definition is uppercased (eg. $foo = System::bar(); System.php) This is what should (and would) work, but currently doesn't. Conclusion: The only problem might be (2): Class name: sample Filename: Sample.php Class definition in Sample.php: class sample { ... } Note: this does work on case insensitive file systems only. I really can't see any reason for maintaining the "Worse is better" principle here, I really doubt that there is much code around relying on the tolowercase feature/bug of spl_autoload(). As a compromise I propose the following: 1) spl_autoload() additionally tries to find a file _not_ lowercased. 2) Throw a E_DEPRECATED in case the filename had to be lowercased to match. Until then: I really don't know why this lowercasing thing was introduced into slp_autoload() to begin with, all it ever did was preventing classes to be named with upper case letters on file systems which are case sensitive. In other words: the only compatibility issue is that code which currently works on platforms like Windows only would suddenly work on UN*X like platforms too. Pls confirm if this is the compatibility issue you are talking about.This seems like a 20 min fix to me, and I've never looked the sql_autoload code before. I spent a good deal of time spinning my wheels on this. I don't see why anyone should lose time of this very obvious bug. However it's better to patch that to bitch, as I always say. I'll submit my patch and see what happens. It's not big deal if this is not fixed, since it's so easy to fix I can keep fixing it with each release. Here is a diff, I'll submit the patch with the "Add a Patch" link. 225d224 < char *lc_class_file; 227d225 < int lc_class_file_len; 229d226 < int mixed_case = 0; 235,236c232 < lc_class_file_len = spprintf(&lc_class_file, 0, "%s%s", lc_name, file_extension); < class_file_len = spprintf(&class_file, 0, "%s%s", class_name, file_extension); --- > class_file_len = spprintf(&class_file, 0, "%s%s", lc_name, file_extension); 252,261c248 < mixed_case = 1; < } < < /* fall back to lowercase file name. should issue deprecated warning. */ < if (ret != SUCCESS) { < ret = php_stream_open_for_zend_ex(lc_class_file, &file_handle, ENFORCE_SAFE_MODE|USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC); < } < < if (ret == SUCCESS) { < if (!file_handle.opened_path && mixed_case == 1) { --- > if (!file_handle.opened_path) { 263,264d249 < } else if(!file_handle.opened_path && mixed_case == 0) { < file_handle.opened_path = estrndup(lc_class_file, lc_class_file_len); 290d274 < efree(lc_class_file); 295d278 < efree(lc_class_file); 331d313 < PS: I really hate this bug with a passion.There's a few things I'd like to add (actually a lot but it's probably best if I keep most of it to myself): sjoerd@php.net: "it will break scripts which depend on spl_autoload being case insensitive." This suggest that right now spl_autoload is in fact case insensitive, which it is not. A case insensitive system should find Core.php when asking for Core.php, just like a case sensitive system would. The difference is that it would ALSO find core.php which would be fine by me. Now it fails to find Core.php making it case destructive at best. wim at asgc dot be: "In addition I would strongly suggest the __autoload function will not be deprecated until this is fixed." Thank god I love irony, however, this won't actually be a problem as you can still use custom auto loaders. All you need to do is register it using spl_autoload_register(). And finally, when using namespaces it is quite easy to get around this problem using a short autoloader function: function SPL_autoload_suxx($class) { include \str_replace('\\', '/', $class) .'.php'; } \spl_autoload_register(__NAMESPACE__ .'\SPL_autoload_suxx'); All you have to do is copy, paste and mop up the river that you've cried.After searching around a while I found a workaround without performance loss. 1. Slower but not working with camelcase class files (like "MyClass.php"): spl_autoload_register(function($classname) { require_once(__DIR__ . '/' . str_replace('\\', '/', $classname) . '.php'); }); 2. Faster but not working with camelcase class files: set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__); spl_autoload_extensions(".php"); spl_autoload_register(); spl_autoload_register( function($classname) {} ); 3. Faster and working with camelcase class files: set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__); spl_autoload_extensions(".php"); spl_autoload_register(function ($classname) { spl_autoload($classname); });One thing everyone here seems to be missing here is that php classes are not case sensitive: This code will execute correctly: class FooBar { } new foobar; new FooBar; new FOOBAR; new fooBar; However, add autoloading into the mix and you create action at a distance that isn't immediately obvious: if the class is in FooBar.php and you have a case-sensitive autoloader then this executes: new FooBar; new foobar; but this does not: new foobar; new FooBar; Should the order of operations here have any affect on whether this script runs successfully or not? Even worse, lets say you have: function a() { new FooBar; } function b(){ new foobar; } function c() { a(); b(); } What's even less obvious is that if the implementation of the a function changes to no longer need the FooBar class, the c function stops working. To the developer working on the a function, the implementation has changed but the API has not so should have no negative effect on anything external, yet another part of the application now breaks for no obvious reason. The PHP Developers are correct that this is not a bug and it's more sensible for the autoloader to be case insensitive. One of two things need to happen: 1) All autoloaders should be case-insensitive OR 2) PHP enforces case sensitivity on all class names.