php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60955 spl_autoload_register() accept a protected method
Submitted: 2012-02-02 14:02 UTC Modified: 2016-06-20 14:26 UTC
From: frederic dot hardy at mageekbox dot net Assigned: cmb (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.3.9 OS: Linux
Private report: No CVE-ID: None
 [2012-02-02 14:02 UTC] frederic dot hardy at mageekbox dot net
Description:
------------
It's possible to register a protected method as an autoloader callback with the function spl_autoload_register().

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

class autoloader
{
	function register()
	{
		spl_autoload_register(array($this, 'requireClass'));
	}

	protected function requireClass() {}
}

$autoloader = new autoloader();
$autoloader->register();

$autoloadFunctions = spl_autoload_functions();

foreach ($autoloadFunctions as $autoloadFunction)
{
	spl_autoload_unregister($autoloadFunction);
}

foreach ($autoloadFunctions as $autoloadFunction)
{
	spl_autoload_register($autoloadFunction);
}

Expected result:
----------------
Cannot register the protected method autoload::requireClass() as a callback

Actual result:
--------------
Passed array does not specify a callable method (cannot access protected method autoloader::requireClass())

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-04 16:06 UTC] php-dev at zerocue dot com
This report is requesting a change in the error output?  It already doesn't allow 
the protected member to be registered as your actual result indicates, it throws 
an error.

What is this report about?
 [2012-02-05 14:15 UTC] frederic dot hardy at mageekbox dot net
All my apologize, my bug report is not very clear.
My problem is that the error message is throwed only if spl_autoload_register() 
was called outsite a class.
A call to autoloader::register() does not throw any error message, even if 
autoloader::requireClass(), a protected method, is used.
 [2012-10-22 11:25 UTC] john dot papaioannou at gmail dot com
Protected methods have been valid as callbacks *if the callback is provided from a scope where the protected method is visible* for quite some time, and this certainly feels "natural".

Why should this not work?

It should also be noted that the line that errors out is the body of the last foreach, which attempts to use the protected method as a callback without being in a scope where the method would be visible.
 [2016-06-20 14:26 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-20 14:26 UTC] cmb@php.net
Indeed, I don't see why this isn't expected behavior.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC