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?

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

x
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
 *
 * 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

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.
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
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
PHP

From novice to tech pro — start learning today.