skaterdav85 Posted September 27, 2012 Share Posted September 27, 2012 (edited) 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 September 27, 2012 by big dave Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2012 Share Posted September 27, 2012 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. Link to comment Share on other sites More sharing options...
boen_robot Posted September 27, 2012 Share Posted September 27, 2012 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 More sharing options...
justsomeguy Posted September 27, 2012 Share Posted September 27, 2012 Where do you have information on that? I see this, but it doesn't cover the case you're using: http://php.net/manua...s.importing.php Link to comment Share on other sites More sharing options...
boen_robot Posted September 27, 2012 Share Posted September 27, 2012 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 More sharing options...
birbal Posted September 27, 2012 Share Posted September 27, 2012 that code is running fine on my machine. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2012 Share Posted September 27, 2012 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2012 Share Posted September 27, 2012 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 More sharing options...
skaterdav85 Posted September 28, 2012 Author Share Posted September 28, 2012 (edited) 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 September 28, 2012 by big dave Link to comment Share on other sites More sharing options...
justsomeguy Posted September 28, 2012 Share Posted September 28, 2012 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. Link to comment Share on other sites More sharing options...
birbal Posted September 28, 2012 Share Posted September 28, 2012 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 More sharing options...
skaterdav85 Posted September 30, 2012 Author Share Posted September 30, 2012 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? Link to comment Share on other sites More sharing options...
birbal Posted September 30, 2012 Share Posted September 30, 2012 <?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 More sharing options...
skaterdav85 Posted October 1, 2012 Author Share Posted October 1, 2012 Interesting, thanks! I used the ReflectionMethod class instead of ReflectionProperty for my protected method, but it worked similarly. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now