Jump to content
skaterdav85

php callback function help

Recommended Posts

In the code sample below, how do I access the value of $this inside the callback function in the hello() method? I also attached the class to this post if you want to execute it. I am getting the error: Fatal error: Using $this when not in object context I want $this to point to the current instance.

class Person {public function hello() {  $list = array(1, 2, 3, 4, 5);  $this->iterate($list, function($num) {   echo $this->multiply_by_100($num) . 'hello <br>';  });}public function iterate($list, $callback) {  foreach ($list as $num) {   call_user_func($callback, $num);  }}public function multiply_by_100($num) {  return $num * 100;}} $d = new Person();$d->hello();

php_closure.php

Edited by big dave

Share this post


Link to post
Share on other sites

I'm not sure you can do that, you would need to tell the function what scope or context to execute in and I'm not sure you can do that in PHP. You can pass $this as a parameter to the function though.

Share this post


Link to post
Share on other sites

You can use the "use" statement, but I think this only became allowed in PHP 5.4, so you might not want to get used to it if you're making apps for servers not under your control.Still... if you have that:

public function hello() {  $list = array(1, 2, 3, 4, 5);  $this->iterate($list, function($num) use ($this) {   echo $this->multiply_by_100($num) . 'hello <br>';  });}

Share this post


Link to post
Share on other sites

I think you meant this:http://php.net/manual/en/functions.anonymous.phpAnd although there's no example, you can see the changelog at the bottom says 5.4 allows $this to be used in anonymous functions.

Share this post


Link to post
Share on other sites

that code is running fine on my machine.

Share this post


Link to post
Share on other sites
I think you meant this:
OK. That's pretty clever, instead of telling it which context to execute the function in you can tell it which local variables should be declared in the function.

Share this post


Link to post
Share on other sites

It looks like the use statement was added in PHP 5.3, so if you want to target that then you can just use a different name for $this.

$obj = $this;$this->iterate($list, function($num) use ($obj) {  echo $obj->multiply_by_100($num) . 'hello <br>';

Share this post


Link to post
Share on other sites

super interesting, thanks everyone for helping with this! EDIT: Just noticed, even though I can pass in $this using the 'use' statement, I can no longer call protected and private members. I have to make them public. I'm guessing it is because the context of the callback function is no longer in the class.

Edited by big dave

Share this post


Link to post
Share on other sites

Right, the context hasn't changed you're just importing local variables into the scope of the function. It's similar to the concept of global variables except the variables aren't global, they're local to another scope.

Share this post


Link to post
Share on other sites
Just noticed, even though I can pass in $this using the 'use' statement, I can no longer call protected and private members. I have to make them public. I'm guessing it is because the context of the callback function is no longer in the class.
you can change the scope only inside the callback function using http://php.net/manual/en/reflectionproperty.setaccessible.php reflection.

Share this post


Link to post
Share on other sites

How would I use the setAccessible method in this case? Do all class members have a static method setAccessible so you can change it's access modifier on the fly?

Share this post


Link to post
Share on other sites
<?phpclass baf{    private $foo='bar';        }$obj=new baf();$obj->get();$prop=new ReflectionProperty('baf','foo');$prop->setAccessible(true);echo $prop->getValue($obj); //prints out private property. same way workswith protected ptoperty?>

yes, you can use it when property is not accessible on the fly. setAccessible() is not static method of properties it is method of ReflectionProperty. we use ReflectionProperty on class property to set or get access to them.

Share this post


Link to post
Share on other sites

Interesting, thanks! I used the ReflectionMethod class instead of ReflectionProperty for my protected method, but it worked similarly.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...