php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61025 __invoke() visibility not honored
Submitted: 2012-02-09 09:17 UTC Modified: 2013-03-08 02:39 UTC
From: jpauli@php.net Assigned: laruence
Status: Closed Package: Class/Object related
PHP Version: 5.3.10 OS: *nix
Private report: No CVE-ID:
 [2012-02-09 09:17 UTC] jpauli@php.net
Description:
------------
__invoke() visibility is not honored when indirectly called as $obj().
It is, when directly called, via $obj->__invoke();

Please, note as well that declaring __invoke() as static works as well, I think 
it shouldn't (nonsense)

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

class Bar {
    private function __invoke() {
        return __CLASS__;
    }
}

$b = new Bar;
echo $b();

/* this works as expected : Fatal Error */
/* echo $b->__invoke(); */

Expected result:
----------------
Call to private method Bar::__invoke() from context ...

Actual result:
--------------
Bar

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-10 22:34 UTC] johannes@php.net
Yes, the current behavior is wrong. I don't think it should be fixed in 5.3 though as the fix might break existing code.
 [2012-07-30 02:22 UTC] willfitch@php.net
johannes - I can commit a fix for this, but at what point should it be introduced?
 [2013-03-06 16:58 UTC] reeze@php.net
Hi, I made a patch again 5.5. how about fix it in 5.5?
 [2013-03-07 03:20 UTC] laruence@php.net
I don't think this need to be fixed in this way,  like __call:
```php
<?php

class Bar {
    private function __call($name, $value) {
        return __CLASS__;
    }
}

$b = new Bar;
$b->__call("name", NULL);
```

works well, but with an warning:
```
Warning: The magic method __call() must have public visibility and cannot be 
static 
```

I am not sure whether this is a bug, or just need document.
 [2013-03-07 14:27 UTC] laruence@php.net
the __toString is more likely:

<?php
class A {
    private function __toString() {
        echo __CLASS__;
    }
}

$a = new A;
echo $a;
echo $a->__toString();
?>

result:

PHP Warning:  The magic method __toString() must have public visibility and 
cannot be static in /tmp/1.php on line 3
A
PHP Fatal error:  Call to private method A::__toString() from context '' in 
/tmp/1.php on line 10


so, I don't see much problem here, I think we only need a warning is okey. since 
it's a magic method, not a normal method.
 [2013-03-08 02:34 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2013-03-08 02:34 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=01049ca7aed40a5f1081d0303c69a40dae0f982e
Log: Fixed bug #61025 (__invoke() visibility not honored)
 [2013-03-08 02:36 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=01049ca7aed40a5f1081d0303c69a40dae0f982e
Log: Fixed bug #61025 (__invoke() visibility not honored)
 [2013-03-08 02:37 UTC] laruence@php.net
-Status: Closed +Status: Open
 [2013-03-08 02:37 UTC] laruence@php.net
jpauli, I commit the patch I said last night, if you want to marco it, please do 
it.

since I need this at trunk before I can improve the __Invoke implemention.

thanks
 [2013-03-08 02:39 UTC] laruence@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence
 [2013-11-17 09:31 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=01049ca7aed40a5f1081d0303c69a40dae0f982e
Log: Fixed bug #61025 (__invoke() visibility not honored)
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 24 19:01:53 2014 UTC