Jump to content

php callback function help


skaterdav85

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
Link to comment
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>';  });}

Link to comment
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.

Link to comment
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>';

Link to comment
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
Link to comment
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.
Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...