php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65042 Protected, private __invoke() magic method raises Warning
Submitted: 2013-06-15 18:40 UTC Modified: 2013-07-03 06:41 UTC
From: pha dot bridgman at gmail dot com Assigned: laruence
Status: Assigned Package: Documentation problem
PHP Version: 5.5.0RC3 OS: Debian 7.0
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

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
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 03:02:42 2014 UTC