php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #65042 Protected, private __invoke() magic method raises Warning
Submitted: 2013-06-15 18:40 UTC Modified: 2018-12-31 20:01 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: pha dot bridgman at gmail dot com Assigned: girgias (profile)
Status: Closed Package: Class/Object related
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
 [2013-06-15 18:40 UTC] pha dot bridgman at gmail dot com
Description:
------------
Definition of a non-public __invoke() magic-method in a class raises a warning.

PHP 5.4 did not exhibit this behaviour, and the manual 
(http://www.php.net/manual/en/language.oop5.magic.php#object.invoke) does not 
stipulate that the __invoke() magic-method must be public.

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

class test
{
    public function __construct()
    {
        $this();
    }

    protected function __invoke()
    {
        echo PHP_VERSION;
    }
}

new test;

?>

Expected result:
----------------
peter@xenon:~/build$ php ~/test/invoke.php 
5.4.4-14

Actual result:
--------------
peter@xenon:~/build/php-5.5.0RC3$ sapi/cli/php ~/test/invoke.php 

Warning: The magic method __invoke() must have public visibility and cannot be 
static in /home/peter/test/invoke.php on line 10
5.5.0RC3

Patches

magic-method-must-constant (last revision 2018-12-30 14:50 UTC by girgias@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-16 13:11 UTC] laruence@php.net
-Package: *General Issues +Package: Documentation problem
 [2013-06-16 16:57 UTC] pha dot bridgman at gmail dot com
Is this really a documentation problem? That would imply that it is intended behaviour for the __invoke() method to always be public! It seems odd that a restriction should be made such that it couldn't have a restricted visibility, like __construct.

In terms of backwards compatibility, if people have been using protected __invoke() methods (because the documentation said they could), 5.5 will now cause warnings, whilst also precluding that functionality. If the alternative, i.e., making the visibility be honoured properly, was employed, then all code 
would function as intended... and we would see more consistency in magic methods.
 [2013-06-16 18:10 UTC] bjori@php.net
zend_error(E_WARNING, "The magic method __call() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __callStatic() must have public 
visibility and be static");
zend_error(E_WARNING, "The magic method __get() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __set() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __unset() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __isset() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __toString() must have public visibility 
and cannot be static");
zend_error(E_WARNING, "The magic method __invoke() must have public visibility 
and cannot be static");
zend_error(E_WARNING, "The magic method __call() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __callStatic() must have public 
visibility and be static");
zend_error(E_WARNING, "The magic method __get() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __set() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __unset() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __isset() must have public visibility and 
cannot be static");
zend_error(E_WARNING, "The magic method __toString() must have public visibility 
and cannot be static");
zend_error(E_WARNING, "The magic method __invoke() must have public visibility 
and cannot be static");
 [2013-06-16 18:14 UTC] bjori@php.net
See also bug#61025
 [2013-06-16 20:19 UTC] pha dot bridgman at gmail dot com
Point taken with regard to consistency; perhaps I would have better expressed myself by saying that allowing magic methods with restricted visibility would be more consistent with the expected operation of class methods.

I understand from reading bug #61025 that in 5.5 (as in previous versions) these magic methods are forcibly public; perhaps as a future project the ability to restrict their visibility could be realised.
 [2013-07-03 06:41 UTC] dsp@php.net
-Assigned To: +Assigned To: laruence
 [2017-01-28 15:55 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Package: Documentation problem +Package: Class/Object related
 [2018-12-30 14:50 UTC] girgias@php.net
The following patch has been added/updated:

Patch Name: magic-method-must-constant
Revision:   1546181419
URL:        https://bugs.php.net/patch-display.php?bug=65042&patch=magic-method-must-constant&revision=1546181419
 [2018-12-30 14:51 UTC] girgias@php.net
-Operating System: Debian 7.0 +Operating System: N/A -PHP Version: 5.5.0RC3 +PHP Version: Irrelevant -Assigned To: laruence +Assigned To: girgias
 [2018-12-31 19:59 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=346507
Log: Fix #65042: Protected, private __invoke() magic method raises Warning

Patch provided by girgias.
 [2018-12-31 20:01 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2018-12-31 20:01 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:05 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=8413d3c303517d5e8307198d74cc227466bf6e28
Log: Fix #65042: Protected, private __invoke() magic method raises Warning
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Aug 12 17:05:45 2022 UTC