Closures

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Closures

Post by Darhazer »

There is no much information in the documentation, so I will ask few questions here.

First, in some languages like JavaScript, you can define 'methods' inside the closure:

Code: Select all

var a = function() {
    function b(){}
}
a.b();
Is it possible in PHP too?
If not, what is the purpose of the Closure class, except for the creating of anonymous function?
what is the purpose to do:

Code: Select all

 
$a = function(){}
What will happen if you try to output for example:

Code: Select all

echo $$a;
Since I have no access to PHP 5.3 yet, I can't play with those code examples :(
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Closures

Post by Weirdan »

It doesn't work in Javascript:

Code: Select all

 
var a = function() { function b() {} }; a.b();
// => TypeError: Object function () { function b() {} } has no method 'b'
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Closures

Post by VladSun »

[js]var a = function() {    function b(){}}a.b();[/js]

In this case I don't think it is a closure but a constructor:

[js]var a = function() {    this.b = function (){}}a.b();[/js]

So, a is an object, and b is its method.

Though, you can define a closure in a closure in JS:

[js]var a = function() {    var c = 'Hello';    var b = function () {         alert(c);    }   b()} [/js]
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Closures

Post by VladSun »

Interesting - does this work in PHP 5.3 ? Anybody?

Code: Select all

class CClass 
{
        public $closure;
        
        public function __construct () 
        {
                $this->closure = function () 
                {
                        echo "Closure";
                };
        }
}
    
$o = new CClass();
$o->closure();
There are 10 types of people in this world, those who understand binary and those who don't
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Closures

Post by SvanteH »

What's the practical use of coding like that?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Closures

Post by VladSun »

SvanteH wrote:What's the practical use of coding like that?
Callbacks:

Code: Select all

usort($array, function ($a, $b) {return $a == $b ? 0 : $a > $b ? -1 : 1});
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Closures

Post by Weirdan »

Code: Select all

 
:!php q.php
string(8) "5.3.0RC4"
 
Fatal error: Call to undefined method CClass::closure() in file.php on line 17
 
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Closures

Post by SvanteH »

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Closures

Post by Weirdan »

Surprisingly, Closure class does not implement __invoke() magic method.'
create-function.php?
No no no, thank you very much.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Closures

Post by VladSun »

Obviously, it's not the same. :)
It's like comparing eval() to plain PHP code :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Closures

Post by kaisellgren »

It does not work in a class context, but works as a simple function call:

Code: Select all

$t = function(){echo 'yebbee';};
$t();
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Closures

Post by VladSun »

kaisellgren wrote:It does not work in a class context, but works as a simple function call
Yes, I knew about it will work as a function call. I just hoped that some "prototype" inheritance could be implemented in PHP :(
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Closures

Post by Jenk »

Do you mean prototype inheritance, or partial classes?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Closures

Post by VladSun »

"prototype inheritance"
I am a big fan of the prototype based OOP :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply