php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41380 Unable to store lambda-style functions in object properties.
Submitted: 2007-05-12 20:51 UTC Modified: 2007-05-14 12:32 UTC
From: stosh1985 at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.2 OS: Mac OS X
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: stosh1985 at gmail dot com
New email:
PHP Version: OS:

 

 [2007-05-12 20:51 UTC] stosh1985 at gmail dot com
Description:
------------
I am unable to store lambda-style function within object properties, like I am with normal variables.  The below code does not work, neither does calling $this->$method() or using call_user_func() with passing in array($this, $this->method) for the callback.

Reproduce code:
---------------
<?php

class foo {
    public $method;

    function __construct() {
        $this->method = create_function('$a, $b', 'print $a . "\n" . $b . "\n\n"; ');

        $this->method('a', 'b');
    }
}

$x = new foo();
$x->method('a', 'b');

?>

Expected result:
----------------
a
b

a
b


Actual result:
--------------
Fatal error: Call to undefined method foo::method

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-14 02:37 UTC] judas dot iscariote at gmail dot com
http://php.net/manual/en/language.oop5.basic.php

"The default value must be a constant expression, not (for example) a variable, a class member or a function call."

This is the expected behaviuor.. a lambda style function is not a constant expression.
 [2007-05-14 12:32 UTC] colder@php.net
His issue is not about default values, by the way, a "lambda-style function" is only a callback, being a string here.

However, your problem comes from the fact that $object->property() is parsed as if property was a method. You'd have to either use

$tmp = $this->method; $tmp('a', 'b');
or call_user_func() / call_user_func_array().

No documentation problem here.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Nov 19 15:00:02 2025 UTC