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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
echo '<pre>';

// StdClass
$s = new stdClass();

$s->sayHello = function() {
    return "Hello World!";


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
Thanks, Ray.
F PCommented:
As of PHP 5.3, you can use anonymous functions like that, yes.

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



Version      Description
5.4.0      $this can be used in anonymous functions.
5.3.0      Anonymous functions become available.
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.

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.
F PCommented:
Agreed 100%.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.