Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to specify this for anonymous functions in PHP

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report