5
<?php $ar = (object) array('a'=>function(){ echo 'TEST'; }); $ar->a(); ?> 

I get this error Call to undefined method

6
  • 1
    probably you are messing this up between javascript syntax style and php syntax style....think again what are you looking for Commented Apr 10, 2012 at 9:52
  • yes , it aint gona work like in javascript indeed. Commented Apr 10, 2012 at 9:58
  • @OMTheEternity closures exist in PHP (php.net/manual/en/functions.anonymous.php). Converting arrays in objects is possible. The question is valid and raises an interesting point in that way of doing things IMO. Commented Apr 10, 2012 at 10:03
  • @Matthieu Thanks For pointing me in right direction.... Commented Apr 10, 2012 at 10:04
  • 1
    Take a look at stackoverflow.com/questions/4535330/… Commented Apr 10, 2012 at 10:08

5 Answers 5

3

Update:

If you are using PHP 5.3 or greater, take a look at other answers please :)


I don't think that's correct syntax, it would give you:

Parse error: syntax error, unexpected T_FUNCTION in.... 

You need to create a class, add method to it, use new keyword to instantiate it and then you will be able to do:

$ar->a(); 

class myclass { public function a() { echo 'TEST'; } } $ar = new myclass; $ar->a(); // TEST 

See Classes and Objects for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

But I try Ahmed Saber is trying to use a closure function instead of declaring the class the way you do.
@ANisus: It looks so but even that would result in Fatal error: Call to undefined method stdClass::a(). @Mukesh has provided an answer below, let's see if this is what he is looking for :)
2

Anonymous or not, you have a callback function, thus you need to handle it as such. E.g.:

<?php $ar = (object) array( 'a' => function(){ echo 'TEST'; } ); call_user_func($ar->a); ?> 

Comments

2

For some reason it doesn't seem possibly to run the closure the way you do. If you modify your code and set another variable to the function, it can be called:

$ar = (object) array('a'=>function(){ echo 'TEST'; }); $a = $ar->a; $a(); 

This is no solution. But from what I can see, this seems like a bug or limitation in PHP 5.3.

I am using 5.3.5 when trying this.

2 Comments

Isn't it $a = $ar->a; instead of $a = $ar->a();? (you fetch the property, which is a closure, and then call it with $a())
Oh, of course it is. Typed too fast. Fixed it now. Thanks for pointing it out.
0

There is no function a() but the property a, so you should call it by $ar->a.
Anyway I don't think it's going to work the way you expect it to.

EDIT: As suggested by Álvaro G. Vicario you should use call_user_func, not echo to call the function and it will work correctly.

Comments

0

Or, just for the fun of it, you can do something like this -

<?php $ar = new stdClass; $ar->a = function($to_echo){ echo $to_echo; }; $temp = $ar->a; //[Edit] - $ar->a("hello"); // doesn't work! php tries to match an instance method called "func" that is not defined in the original class' signature $temp("Hey there"); call_user_func($ar->a("You still there?")); ?> 

2 Comments

Fatal error: Call to undefined method stdClass::a() in my 5.3 box.
ok, so you have to assign it to another variable to get the function call to work. or use your method - call_user_func($ar->a). got it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.