• 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Declaring an anonymous function within new stdClass

Just wondering why something like this doesn't work:

public function address($name){
        $address = new stdClass();
        $address->city = function($class = '', $style = ''){
            return $class;
        $this->addresses[$name] = $address;
    return $this->addresses[$name];

Calling it like echo $class->address('name')->city('Class') should just echo Class, however I get Fatal error: Call to undefined method stdClass::city()

I can find a better way to do this, because this will get messy, but I'm wondering what I might be doing wrong there, or if PHP doesn't support this and why.

PHP is right when invoke fatal error Call to undefined method stdClass::city() because object $class->address('name') has no method city. Intead, this object has property city which is instance of Closure Class (http://www.php.net/manual/en/class.closure.php) You can verify this: var_dump($class->address('name')->city)

I found the way to call this anonymous function is:

$closure = $class->address('name')->city;

Hope this helps!

  • 8
Reply Report

Sadly it is not possible within stdClass, but there is a workaround -- PHP Anonymous Object.

// define by passing in constructor
$anonim_obj = new AnObj(array(
    "foo" => function() { echo "foo"; }, 
    "bar" => function($bar) { echo $bar; } 

$anonim_obj->foo(); // prints "foo"
$anonim_obj->bar("hello, world"); // prints "hello, world"
  • 5
Reply Report

AFAIK, this is not supported by PHP, and you must use the call_user_func() or call_user_func_array() functions to call closures assigned to class properties (usually you can use __call() to do this, but in your case, the class is stdClass, so this isn't possible).

  • 2
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions