php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53683 Presence of __invoke makes protected __construct visible as callable
Submitted: 2011-01-07 01:02 UTC Modified: 2011-01-28 22:33 UTC
From: tomasz dot slominski at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.5 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tomasz dot slominski at gmail dot com
New email:
PHP Version: OS:

 

 [2011-01-07 01:02 UTC] tomasz dot slominski at gmail dot com
Description:
------------
Presence of __invoke causes  is_callable($obj, '__construct') returns true, 
instead of false when it's called out of class scope.

Also, is_callable('class', '__construct') yields true even if __construct is set 
to protected (which is clearly not connected with __invoke issue, but I suppose 
that is caused by the same underlying problem).

Although setting __construct private/protected seems a little bit pervert, if it 
is not interdicted by language semantics, visibility testing should give good 
results. 

Test script:
---------------
class Singleton {
		protected static $aInstances = array();
		final protected function __construct(){}					
		public static function getInstance(){
    		$sClassName = get_called_class();
			if (!isset(static::$aInstances[$sClassName])) {
				static::$aInstances[$sClassName] = new $sClassName();		
			} // if
        	return static::$aInstances[$sClassName];		
		} // getInstance
	} // class
	
	class SingletonWithInvoke extends Singleton {			
		final protected function __invoke(){}	
	} // class
			
	var_dump(is_callable(Singleton::getInstance(), '__construct'));
	var_dump(is_callable(SingletonWithInvoke::getInstance(), '__construct'));
	var_dump(is_callable('Singleton', '__construct'));	
	var_dump(is_callable('SingletonWithInvoke', '__construct'));

Expected result:
----------------
bool(false) bool(false) bool(false) bool(false)

Actual result:
--------------
bool(false) bool(true) bool(true) bool(true) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-28 22:33 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-01-28 22:33 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See your code, you are using is_callable('foo', 'bar') instead of is_callable(array('foo', 'bar'))... :)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 16:01:33 2025 UTC