PHP, is it possible to store a function in stdClass?

Look at this code snippet:

$s = new stdClass();
    $s->sayHello = function() {
        echo "Hello World!";
    }; // This definition would not get an error
    
    $s->sayHello(); // This line would get an error

Open in new window


Somehow, I am able to store a function in stdClass without any errors. But when I try to invoke the function, I get this error message:

PHP Fatal error:  Call to undefined method stdClass::sayHello()

Open in new window


I wish it would give me an error when I defined the function, then I'd know I can't do such a thing. But to let me store a function in stdClass and then give me an error gives me the impression that I'm just not doing something right. So what am I doing wrong?

Thanks.
elepilAsked:
Who is Participating?
 
Ray PaseurCommented:
"Wrong" is probably too strong a term.  You can do this in many languages, especially those with prototypal inheritance like JavaScript.  But if it doesn't work in PHP, you can just define your own class.  PHP StdClass is a built-in and I would not be inclined to modify it.

This may not do what you want but at least it shows what the object looks like after the injection.
<?php // demo/temp_elepil.php

/**
 * How to understand closure injection
 *
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28670777.html
 */
error_reporting(E_ALL);
echo '<pre>';

// StdClass
$s = new stdClass();

// ADD A CLOSURE
$s->sayHello = function() {
    return "Hello World!";
};

// SHOW THE CLASS WITH THE CLOSURE IN IT
var_dump($s);

Open in new window

0
 
elepilAuthor Commented:
Thanks, Ray.
0
 
F PCommented:
As of PHP 5.3, you can use anonymous functions like that, yes.

http://php.net/manual/en/functions.anonymous.php

$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');

Changelog

Version      Description
5.4.0      $this can be used in anonymous functions.
5.3.0      Anonymous functions become available.
0
 
Ray PaseurCommented:
Anonymous functions are one thing and they seem fine in procedural code; getting them to work sensibly with StdClass is another.  Also, PHP built-in classes have some "interesting" side effects when you try to use the object they create.
http://php.net/manual/en/function.unserialize.php#112823

Closures have some of the characteristics of programming and some of the characteristics of data.  Is a closure that was added to an object a method or a property of the object?  The behavior and documentation do not make this difference clear (at least not to me).  So I think that the right practice might be to avoid any ambiguity and risk.  It's so easy to create our own classes, and their behavior is predictable and sensible.
0
 
F PCommented:
Agreed 100%.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.