Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to specify this for anonymous functions in PHP

Shulou Source: shulou.com Published: 2022-06-02 06:50:38 09月25日 Update

This article introduces how to specify this for anonymous functions in PHP. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

How do I specify this for anonymous functions in PHP?

In the previous article, we have learned about the use of anonymous functions. Friends who have not seen it can enter the portal to learn about the use of anonymous functions in closures. Transfer: do not know that PHP has closures? Then you are really OUT.

A typical problem with anonymous functions with closures in JS is to bind it to a this scope. In fact, this problem also exists in PHP, such as the following code:

$func = function ($say) {

Echo $this- > name,':', $say, PHP_EOL

}

$func ('good'); / / Fatal error: Uncaught Error: Using $this when not in object context

In this anonymous function, we use\ $this- > name to get the $name attribute in the current scope, but who is this $this? We don't define it, so we will report an error directly here. The error message is that $this is used but there is no object context, which means that the scope of the $this reference is not specified.

The bindTo () method binds $this

Well, let's just give it a scope, just like JS, using a bindTo () method.

$func1 = $func- > bindTo ($lily, 'Lily')

/ / $func1 = $func- > bindTo ($lily, Lily::class)

/ / $func1 = $func- > bindTo ($lily, $lily)

$func1 ('cool')

This time the output can be normal. The bindTo () method copies a current closure object and binds it with $this scope and class scope. Where the $lily parameter is an object $newthis parameter, which specifies $this for the copied anonymous function. The second parameter, 'Lily', binds to a new class scope, which represents a type and determines which private and protected methods can be called in this anonymous function. All three ways given in the above example can be used to define this parameter. If we don't give this parameter, then we can't access the $name property of the private:

$func2 = $func- > bindTo ($lily)

$func2 ('cool2'); / / Fatal error: Uncaught Error: Cannot access private property Lily::$name

The call () method binds $this

After PHP7, PHP added the call () method to bind anonymous functions to $this. Let's see how it differs from the bindTo () method.

$func- > call ($lily, 'well'); / / Lily:well

So much for sharing on how to specify this for anonymous functions in PHP. I hope the above can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Tags: Functions actions methods parameters closures that is content objects boys buddies properties articles more questions help good up and down context that is code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei Microsoft Shulou Technology Shulou Information