Bug #61422 [NEW]: Lack of autoload on type hinting breaks class_alias

From: Date: Fri, 16 Mar 2012 21:06:28 +0000
Subject: Bug #61422 [NEW]: Lack of autoload on type hinting breaks class_alias
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-168560@lists.php.net to get a copy of this message
From:             
Operating system: Linux
PHP version:      5.4.0
Package:          SPL related
Bug Type:         Bug
Bug description:Lack of autoload on type hinting breaks class_alias

Description:
------------
I found #39003 which implies that autoload *was* called for type hinting
previously and called for removal of this "unnecessary" feature.

I beg to differ. Our framework depends on using class_alias to provide a
dynamic modular structure, and one real class may have multiple aliases
depending on situation, and these aliases are currently added when needed.
Therefore, if an object is creating using alias X (or no alias at all) and
is passed to a function expecting the same class under alias Y (and no
object was created using that alias) a completely invalid fatal error is
raised (see a simplified demo below).

I would also like to point out, that if after calling autoload it turns out
that it was not, in fact, an alias, the call will fatally fail anyway, so a
really useless autoload will be only called once. I don't see any "resource
consumption" problem here.

Test script:
---------------
<?

	class Demo { }
	
	// An "autoload" handler creating an alias for the same class
	spl_autoload_register(function ($class) {
		class_alias('Demo', $class);
	});
	
	// Create a Demo object using "A"
	// and pass that to function expecting "A"
	function success(A $test) {
		echo "Success ".get_class($test)."\n";
	}
	success(new A());
	
	// Create a Demo object using "B",
	// then create another Demo object using "C"
	// and pass that to function expecting "B"
	function also(B $test) {
		echo "Success ".get_class($test)."\n";
	}
	new B();
	also(new C());
	
	// Create a Demo object using "X",
	// and pass that to function expecting "B"
	function fail(X $test) {
		echo "Success ".get_class($test)."\n";
	}
	
	fail(new Y());
	
	// Note that all of these names refer to the same class,
	// and all objects are of the same class



Expected result:
----------------
Success Demo
Success Demo
Success Demo

Actual result:
--------------
Success Demo
Success Demo
PHP Catchable fatal error:  Argument 1 passed to fail() must be an instance
of X, instance of Demo given

-- 
Edit bug report at https://bugs.php.net/bug.php?id=61422&edit=1
-- 
Try a snapshot (PHP 5.4):            https://bugs.php.net/fix.php?id=61422&r=trysnapshot54
Try a snapshot (PHP 5.3):            https://bugs.php.net/fix.php?id=61422&r=trysnapshot53
Try a snapshot (trunk):              https://bugs.php.net/fix.php?id=61422&r=trysnapshottrunk
Fixed in SVN:                        https://bugs.php.net/fix.php?id=61422&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=61422&r=needdocs
Fixed in release:                    https://bugs.php.net/fix.php?id=61422&r=alreadyfixed
Need backtrace:                      https://bugs.php.net/fix.php?id=61422&r=needtrace
Need Reproduce Script:               https://bugs.php.net/fix.php?id=61422&r=needscript
Try newer version:                   https://bugs.php.net/fix.php?id=61422&r=oldversion
Not developer issue:                 https://bugs.php.net/fix.php?id=61422&r=support
Expected behavior:                   https://bugs.php.net/fix.php?id=61422&r=notwrong
Not enough info:                     https://bugs.php.net/fix.php?id=61422&r=notenoughinfo
Submitted twice:                     https://bugs.php.net/fix.php?id=61422&r=submittedtwice
register_globals:                    https://bugs.php.net/fix.php?id=61422&r=globals
PHP 4 support discontinued:          https://bugs.php.net/fix.php?id=61422&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=61422&r=dst
IIS Stability:                       https://bugs.php.net/fix.php?id=61422&r=isapi
Install GNU Sed:                     https://bugs.php.net/fix.php?id=61422&r=gnused
Floating point limitations:          https://bugs.php.net/fix.php?id=61422&r=float
No Zend Extensions:                  https://bugs.php.net/fix.php?id=61422&r=nozend
MySQL Configuration Error:           https://bugs.php.net/fix.php?id=61422&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#168560) next »