Page 1 of 1

Closures

Posted: Wed Jul 08, 2009 5:52 am
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 :(

Re: Closures

Posted: Wed Jul 08, 2009 6:13 am
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'
 

Re: Closures

Posted: Wed Jul 08, 2009 6:23 am
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]

Re: Closures

Posted: Wed Jul 08, 2009 6:29 am
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();

Re: Closures

Posted: Wed Jul 08, 2009 6:41 am
by SvanteH
What's the practical use of coding like that?

Re: Closures

Posted: Wed Jul 08, 2009 6:46 am
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});

Re: Closures

Posted: Wed Jul 08, 2009 6:50 am
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
 

Re: Closures

Posted: Wed Jul 08, 2009 6:53 am
by SvanteH

Re: Closures

Posted: Wed Jul 08, 2009 6:54 am
by Weirdan
Surprisingly, Closure class does not implement __invoke() magic method.'
create-function.php?
No no no, thank you very much.

Re: Closures

Posted: Wed Jul 08, 2009 6:56 am
by VladSun
Obviously, it's not the same. :)
It's like comparing eval() to plain PHP code :)

Re: Closures

Posted: Wed Jul 08, 2009 1:11 pm
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();

Re: Closures

Posted: Wed Jul 08, 2009 4:37 pm
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 :(

Re: Closures

Posted: Thu Jul 09, 2009 6:36 am
by Jenk
Do you mean prototype inheritance, or partial classes?

Re: Closures

Posted: Thu Jul 09, 2009 6:38 am
by VladSun
"prototype inheritance"
I am a big fan of the prototype based OOP :)