php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #47765 __invoke lacks if object is a static property or return
Submitted: 2009-03-24 19:10 UTC Modified: 2009-11-19 13:23 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: biohazard999 at gmx dot at Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3.0RC1 OS: *
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: biohazard999 at gmx dot at
New email:
PHP Version: OS:

 

 [2009-03-24 19:10 UTC] biohazard999 at gmx dot at
Description:
------------
If the object to invoke is stored in a static property or is returned by a static function __invoke will not be called.
Especially for singletons is this extremly missleading.

Either it is an documentation problem or something other is wrong.

Actual documentation:
The __invoke method is called when a script tries to call an object as a function. 

Reproduce code:
---------------
class foo
{
  public static $instance = null; //only public for this example

  public static function get()
  {
    if(is_null(self::$instance))
      self::$instance = new self;

    return self::$instance;
  }

  public function __invoke($name)
  {
    echo 'Hello '.$name;
  }
}

//1)
foo::get()('World');

//2)
foo::get();
foo::$instance('World');

//3)
$bar = foo::get();
$bar('World');

Expected result:
----------------
1) //Here is the parsing-error partial expected
Hello World

2)
Hello World

3)
Hello World

Actual result:
--------------
1)
Parse error: parse error in test.php on line 23

2)
Notice: Undefined variable: instance in test.php on line 27
Fatal error: Function name must be a string in test.php on line 27

3)
Hello World //Behavior as expected

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-18 15:18 UTC] pajoye@php.net
not windows specific
 [2009-11-19 11:24 UTC] vrana@php.net
It's because of priority:
<?php
$instance = "f";
foo::$instance('World'); // calls foo::f('World')
?>

$instance('World') has higher priority than foo::$instance.
 [2009-11-19 13:23 UTC] biohazard999 at gmx dot at
There is no method called "f". I'm talking about a static member variable.

So you can't invoke a static member variable as function.
There is NO hint in the documentation you can't call a object thats returned by a method or holden in a static member var as a function.

Check the docs:
The __invoke method is called when a script tries to call an object as a function. 

$anObject(); //calls: __invoke
$this->anObject(); //calls: __invoke
AClass::$anObject(); //throws: Fatal error?! there is no static member "anObject". 
AClass::methodReturningAnObject()() //should: call the __invoke operation on the returned object. throws: parsing error

$anObject = AClass::methodReturningAnObject();
$anObject(); //calls: __invoke(); -> works like the first example.


$member = 'methodReturningAnObject';
$anObject = AClass::$member(); // calls AClass::methodReturningAnObject(); returns: $anObject.
$anObject(); //calls: __invoke(); -> works like the first example.

BUT:
AClass::$member()(); //should: call the __invoke operation on the returned object. throws: parsing error.

Thats all logical, but not documentated. 
An object is still a object, independent from the "storing point"
This is a documentation problem. Not a bogus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 05:01:37 2025 UTC