#47765 [Opn]: __invoke lacks if object is a static property or return

From: Date: Thu, 18 Jun 2009 15:18:45 +0000
Subject: #47765 [Opn]: __invoke lacks if object is a static property or return
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-2370@lists.php.net to get a copy of this message
 ID:               47765
 Updated by:       pajoye@php.net
 Reported By:      biohazard999 at gmx dot at
 Status:           Open
 Bug Type:         Documentation problem
-Operating System: Windows XP SP3
+Operating System: *
 PHP Version:      5.3.0RC1
 New Comment:

not windows specific


Previous Comments:
------------------------------------------------------------------------

[2009-03-24 19:10:07] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=47765&edit=1



Thread (4 messages)

« previous php.doc.bugs (#2370) next »