How to access parent object from lambda functions? - php

I have a recursive lambda function in one of my objects, and it needs to access the object's mysqli connection. This attempt
$recfunc = function($id, $name) use($this) {
Produced an unreasonable fatal error
Fatal error: Cannot use $this as lexical variable in C:\Users\Codemonkey1991\Desktop\workspace\melior\objects\databasemanager.php on line 88
Could anyone give me a few pointers?
Edit: Just to clarify context, I'm trying to create this lambda function inside another function.

Because closures are themselves objects, you need to assign $this to a local variable, like:
$host = $this;
$recfunc = function($id, $name) use ($host) { ...

The reference to $this does not need to be explicitly passed to the lambda function.
class Foo {
public $var = '';
public function bar() {
$func = function() {
echo $this->var;
};
$func();
}
}
$foo = new Foo();
$foo->var = 'It works!';
$foo->bar(); // will echo 'It works!'

Related

PHP function inside another function cannot reference `$this`

I have a function inside a function and the inner function cannot find the outer class. I don't know why. I hope someone has an idea how I can solve this. I don't want to declare the inner function outside of the outer function.
class Foo {
public $test = "test123";
private function outerfunction(){
function innerfunction(){
echo $this->test;
}
innerfunction();
}
}
if I call the inner function I get the error Using $this when not in object context.
just restructure it to an anonymous function:
$innerfunction = function () {
echo $this->test;
}
$innerfunction();
The class context is then automatically bound to the function (see also http://php.net/manual/en/functions.anonymous.php)
Be aware, that this only works for >= PHP 5.4
You have to declare it as a variable, or it will tell you that you aren't in an object context:
$innerfunction = function() {
echo $this->test;
};
call_user_func($innerfunction);
Alternatively, you can keep the function as you do, and pass it the object context since it's known at the call point:
function innerfunction(/* Foo */ $obj) {
echo $obj->test;
};
innerfunction($this);
The most glaring issue with this approach is that calling outerFunction() more than once will produce:
Fatal error: Cannot redeclare innerfunction()...
Another issue is that innerfunction() is declared in the global scope. If this is intentional then that is highly unconventional and you probably should not do that. Anyways, it allows for this:
class Foo {
public $test = "test123";
public function outerfunction(){
function innerfunction(){
//echo $this->test;
}
innerfunction();
}
}
$a = new foo();
// innerfunction(); // NOPE!
$a->outerFunction();
innerfunction(); // YES
So, this is the logical way to declare innerfunction():
class Foo {
public $test = "test123";
public function outerfunction(){
$this->innerfunction();
}
private function innerfunction(){
echo $this->test;
}
}
$a = new foo();
$a->outerFunction();

PHP: assign function to class method

How can I assign a function to a method in a class in PHP? I tried the following:
class Something{
public function __construct(){
$functionNames = array('foo', 'bar')
$variable = 'blablabla';
foreach($functionNames as $functionName){
if(method_exists($this, $functionName))
continue;
$this->{$functionName}() = function($params){ //should create the methods "foo" and "bar"
echo $variable; //should echo 'blablabla' (I know that the variable was declared outside this function, but how can I access it anyway?)
}; //the error points to here
}
}
}
But this code gives me this error:
Fatal error: Can't use method return value in write context
Does anyone know how I can assign the anonymous function to the class method, while also still being able to access variables outside that function?
You are doing foreach($functionNames as $functionName){ which means that $functionName is a string, not an array. So, don't use $functionName[0].
method_exists takes 2 parameters. One is the object and the other is the method name. It should be:
method_exists($this, $functionName)
As for creating the function, you don't need () on the left side of the =. It should be:
$this->$functionName = function($params) use($variable){
echo $variable;
};
The use($variable) is needed to tell PHP to use that variable inside the function. That's how closures work in PHP, it's different than other languages.
So, your class should look like:
class Something{
public function __construct(){
$functionNames = array('foo', 'bar');
$variable = 'blablabla';
foreach($functionNames as $functionName){
if(method_exists($this, $functionName)){
continue;
}
$this->$functionName = function($params) use($variable){
echo $variable;
};
}
}
}
Problem here is that in this way of making functions, you are not actually creating a class method, but instead creating a class variable that contains a function.
So, you need to call it like so:
$test = new Something;
$foo = $test->foo;
$foo('abc');
You can't just do $test->foo('abc');.
EDIT: Another thing you can do is use PHP's __call "magic method". This will be ran whenever you do ->funcName(), regardless of whether the method exists or not. Using that method, you can just check to see if the method called was 'foo' or 'bar'. See this example:
class Something{
private $variable;
public function __construct(){
$this->variable = 'blablabla';
}
public function __call($name, $params=array()){
if(method_exists($this, $name)){
// This makes sure methods that *do* exist continue to work
return call_user_func(array($this, $name), $params);
}
else{
$functionNames = array('foo', 'bar');
if(in_array($name, $functionNames)){
// You called ->foo() or ->bar(), so do something
// If you'd like you can call another method in the class
echo $this->variable;
}
}
}
}
With this, now you can do the following:
$test = new Something;
$test->foo('abc'); // Will echo "blablabla"

PHP invoke method using string for method name

I would like to have an array of methods in my php class, indexed with method names, so that I can do something like this:
public function executeMethod($methodName){
$method=$this->methodArray[$methodName];
$this->$method();
// or some other way to call a method whose name is stored in variable $methodName
}
I've found this for __call:
The overloading methods are invoked when interacting with properties
or methods that have not been declared or are not visible in the
current scope
However, methods I'd like to use in executeMethod are visible.
What is proper way to do that? Is it possible?
EDIT: I wanted to get a method name in the executeMethod, and then call the method of the given name, and had an idea of methods array.
you can call object methods and properties by using string with syntax
$method = 'your_method_name_as_string';
$this->$method();
from php doc
<?php
class Foo
{
function Variable()
{
$name = 'Bar';
$this->$name(); // This calls the Bar() method
}
function Bar()
{
echo "This is Bar";
}
}
$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // This calls $foo->Variable()
?>
Maybe you are looking for something like this:
public function executeMethod($methodName) {
if (isset($this->methodArray[$methodName])) {
$method = $this->methodArray[$methodName];
return call_user_func(array($this, $method));
}
throw new Exception("There is no such method!");
}
anonymous functions are available in php 5.3
i think you're trying to do something like
$tmp['doo'] = function() { echo "DOO"; };
$tmp['foo'] = function() { echo "FOO"; };
$tmp['goo'] = function() { echo "GOO"; };
$tmp['doo']();

Does a PHP static anonymous function really work?

I'm trying to learn PHP, and now I'm stuck in 'static anonymous function'.
I found this in a tutorial (http://www.slideshare.net/melechi/php-53-part-2-lambda-functions-closures-presentation)
"Object Orientation
Lambda Functions are Closures because they automatically get bound to the scope of the class that they are created in.
'$this' is not always needed in the scope.
Removing '$this' can save on memory.
You can block this behaviour by declaring the Lambda Function as static."
What is wrong with this code?
I get this error:
Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in C:\wamp\www\z-final\a.php on line 11
Why this code line doesn't work "return static function(){var_dump($this);};" ?
class foo
{
public function getLambda()
{
return function(){var_dump($this);};
}
public function getStaticLambda()
{
return static function(){var_dump($this);};
}
}
$foo = new foo();
$lambda = $foo->getLambda();
$staticLambda = $foo->getStaticLambda();
$lambda();
$staticLambda();
Yes, that is perfectly valid syntax in 5.4+.
Basically, it prevents auto-binding of the current class to the closure (in fact, it prevents all binding, but more on that later).
class Foo {
public function bar() {
return static function() { var_dump($this); };
}
public function baz() {
return function() { var_dump($this); };
}
}
If we instantiate that on 5.4+, the closure bar() returns will have $this set to null. Just as if you made a static call to it. But baz() would have $this set to the foo instance you called baz() on.
So:
$bar = $f->bar();
$bar();
Results in:
Notice: Undefined variable: this in /in/Bpd3d on line 5
NULL
And
$baz = $f->baz();
$baz();
Results in
object(Foo)#1 (0) {
}
Make sense? Great.
Now, what happens if we take closures defined outside of a function:
$a = function() { var_dump($this); };
$a();
We get null (and a notice)
$c = $a->bindTo(new StdClass());
$c();
We get StdClass, just as you'd expect
$b = static function() { var_dump($this); };
$b();
We get null (and a notice)
$d = $b->bindTo(new StdClass());
$d();
This is where things get interesting. Now, we get a warning, a notice, and null:
Warning: Cannot bind an instance to a static closure in /in/h63iF on line 12
Notice: Undefined variable: this in /in/h63iF on line 9
NULL
So in 5.4+, you can declare a static closure, which results in it never getting $this bound to it, nor can you ever bind an object to it...
There should be no need to define it with the static keyword.
<?php
class House
{
public function paint($color)
{
return function() use ($color) { return "Painting the house $color..."; };
}
}
$house = new House();
$callback = $house->paint('red');
var_dump($callback); // object(Closure)#2 (2) {..}
var_dump($callback()); // "Painting the house red..."

Add method in an std object in php

Is it possible to add a method/function in this way, like
$arr = array(
"nid"=> 20,
"title" => "Something",
"value" => "Something else",
"my_method" => function($arg){....}
);
or maybe like this
$node = (object) $arr;
$node->my_method=function($arg){...};
and if it's possible then how can I use that function/method?
This is now possible to achieve in PHP 7.1 with anonymous classes
$node = new class {
public $property;
public function myMethod($arg) {
...
}
};
// and access them,
$node->property;
$node->myMethod('arg');
You cannot dynamically add a method to the stdClass and execute it in the normal fashion. However, there are a few things you can do.
In your first example, you're creating a closure. You can execute that closure by issuing the command:
$arr['my_method']('Argument')
You can create a stdClass object and assign a closure to one of its properties, but due to a syntax conflict, you cannot directly execute it. Instead, you would have to do something like:
$node = new stdClass();
$node->method = function($arg) { ... }
$func = $node->method;
$func('Argument');
Attempting
$node->method('Argument')
would generate an error, because no method "method" exists on a stdClass.
See this SO answer for some slick hackery using the magic method __call.
Since PHP 7 it is also possible to directly invoke an anonymous function property:
$obj = new stdClass;
$obj->printMessage = function($message) { echo $message . "\n"; };
echo ($obj->printMessage)('Hello World'); // Hello World
Here the expression $obj->printMessage results in the anonymous function which is then directly executed with the argument 'Hello World'. It is however necessary to put the function expression in paranetheses before invoking it so the following will still fail:
echo $obj->printMessage('Hello World');
// Fatal error: Uncaught Error: Call to undefined method stdClass::printMessage()
Another solution would be to create an anonymous class and proxy the call via the magic function __call, with arrow functions you can even keep reference to context variables:
new Class ((new ReflectionClass("MyClass"))->getProperty("myProperty")) {
public function __construct(ReflectionProperty $ref)
{
$this->setAccessible = fn($o) => $ref->setAccessible($o);
$this->isInitialized = fn($o) => $ref->isInitialized($o);
$this->getValue = fn($o) => $ref->getValue($o);
}
public function __call($name, $arguments)
{
$fn = $this->$name;
return $fn(...$arguments);
}
}
class myclass {
function __call($method, $args) {
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}
}
$obj = new myclass();
$obj->method = function($var) { echo $var; };
$obj->method('a');
Or you can create defult class and use...

Categories