php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63482 register_tick_function() does not work correctly with private methods
Submitted: 2012-11-10 19:12 UTC Modified: 2012-11-10 20:42 UTC
From: dev at pp3345 dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.8 OS: Debian Squeeze
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dev at pp3345 dot de
New email:
PHP Version: OS:

 

 [2012-11-10 19:12 UTC] dev at pp3345 dot de
Description:
------------
Registering a private method as a tick function leads to two errors on every tick.

Test script:
---------------
<?php
declare(ticks = 1);

class A {
        public function __construct() {
                register_tick_function(array($this, 'a'));
        }

        private function a() {
                echo 'Called';
        }
}

new A;

?>


Expected result:
----------------
CalledCalledCalled

Actual result:
--------------
Called
Warning: Invalid callback A::a, cannot access private method A::a() in 
/var/vhosts/pancake/default/register_tick_function.php on line 15

Warning: main(): Unable to call A::a() - function does not exist in 
/var/vhosts/pancake/default/register_tick_function.php on line 15

Warning: Invalid callback A::a, cannot access private method A::a() in 
/var/vhosts/pancake/default/register_tick_function.php on line 17

Warning: main(): Unable to call A::a() - function does not exist in 
/var/vhosts/pancake/default/register_tick_function.php on line 17

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-10 20:42 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-11-10 20:42 UTC] nikic@php.net
As the tick function is not called in the class scope it also can't access its private methods. That's the whole point of private methods. This is also not specific to ticks, its the same with all private-method callbacks: They will only work if they are invoked from within the class.
 [2012-11-10 21:11 UTC] dev at pp3345 dot de
I understand that private methods can not be called from outside the class. But I 
believe that register_tick_function() should disallow registering private or 
protected methods or at least there should only be ONE error after leaving the 
class scope. Probably PHP should automatically recognize that the class scope was 
left, unregister the tick function and issue a notice.

As far as I remember, private methods are fully supported as class autoloaders and 
work even when loading a class outside the class scope of the registered private 
method. This behavior is somewhat inconsistent.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 18:01:32 2025 UTC