TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Anonymous Functions in PHP 5.3.0, deprecates create_function (http://www.talkphp.com/general/4661-anonymous-functions-php-5-3-0-deprecates-create_function.html)

Orc 07-01-2009 11:22 PM

Anonymous Functions in PHP 5.3.0, deprecates create_function
 
I've seen in PHP 5.3.0 which has been recently released, allows for anonymous functions assigned to variables, so instead of doing this:

Example #1
PHP Code:

function Add($a,$b) { return $a $b; }

$A 'Add';
$var1 2;
$var2 2;

printf('%d'$A($var1,$var2)); 

Example #2
PHP Code:


$A 
create_function('$args''return $args[0] + $args[1];');

printf('%d'$A(2,2)); 


We can do:
PHP Code:

$A = function($a$b) {
 return 
$a $b;


which I like this a lot, reminds me a lot like javascript, i hope this can be done with classes aswell, like so:
PHP Code:


$A
::MyMethod = public static function( $a$b ) { return $a b; } 

On-Topic about PHP 5.3 aswell, I tried to install it on xampp, but I still happen to get a parse error by trying to do the simplest of putting in "namespace Project;". even though the module is loaded in apache ( at least in the config )

codefreek 07-01-2009 11:49 PM

Quote:

Originally Posted by Orc (Post 26442)


On-Topic about PHP 5.3 aswell, I tried to install it on xampp, but I still happen to get a parse error by trying to do the simplest of putting in "namespace Project;". even though the module is loaded in apache ( at least in the config )


You have to wait for xampp to release an update, the module still needs to be coded into the program, thats why..

ryanmr 07-02-2009 04:43 AM

Now that 5.3.0 has been released, what do you think the Host penetration time will be? As in, how long do you think it will take for shared hosts to update their PHP version?

I have 1and1 and I don't think they'll be fast about updating anything.

Orc 07-02-2009 09:25 AM

Quote:

Originally Posted by codefreek (Post 26444)
You have to wait for xampp to release an update, the module still needs to be coded into the program, thats why..

But I had installed VC6 Thread safe..

Orc 07-02-2009 09:28 AM

Quote:

Originally Posted by ryanmr (Post 26464)
Now that 5.3.0 has been released, what do you think the Host penetration time will be? As in, how long do you think it will take for shared hosts to update their PHP version?

I have 1and1 and I don't think they'll be fast about updating anything.

It probably won't be but for a very long time. :P But I'm really looking forward to namespaces, it's one of those things I wanted in PHP, that I always seemed to need. plus it stops those people from whining about how php isnt really a language without namespaces ;p

Tanax 07-02-2009 10:44 AM

Something quite cool is also the use keyword. I don't know if it's specifically for the anonymous functions, but it's great 'cause then you can pass in variables into functions that you need, but don't want to pass as a param when calling the function.

PHP Code:


$db 
= new DB();

$connect = function($host$user$pass) use($db) {

$db->connect($host$user$pass);

}

$connect('localhost''root'''); 


Wildhoney 07-02-2009 11:22 AM

I think use will also be used to specify which namespaces you're wanting to use. I like that feature in anonymous functions!

Kalle 07-02-2009 03:00 PM

Actually we didn't deprecate create_function() (I however suggested it that we did on IRC), the create_function is horrible, what it internally does is to eval() the code and create a runtime function (named __lambda_func) with a hash returned, so its horrible slow and can easily be injected as theres no protection at the eval.

Closures with 5.3 is however really nicely implemented, they are actually implemented in an object oriented way, meaning that:
PHP Code:

<?php
$closure 
= function() { echo 'Hello'; };
?>

Is the same as:
PHP Code:

<?php
$closure 
= new Closure;

/* Where the Closure class would virtually be populated like */
class Closure
{
    public function 
__invoke()
    {
        echo 
'Hello';
    }
}
?>

Meaning that $closure is actually a virtual object created with some engine magioc, using the new magic method __invoke(). The __invoke() method can also be used on regular objects allowing them to act as callables/closures:

PHP Code:

<?php
class Welcomer
{
    public function 
__invoke($name)
    {
        
printf('Greetings %s from the TalkPHP community'$name);
    }
}

$welcomer = new Welcomer;

$welcomer('Kalle');
$welcomer('Adam');
$welcomer('Peter');
?>

So all in all, Closures are damn sleek and is without doubt my favorite feature of PHP 5.3 =)

As for the Windows stuff, just configure your own Apache and load the PHP dll, takes two minutes and whoop whoop you have it running (but thats just me who does it manually =P)

codefreek 07-02-2009 03:09 PM

Quote:

Originally Posted by ryanmr (Post 26464)
Now that 5.3.0 has been released, what do you think the Host penetration time will be? As in, how long do you think it will take for shared hosts to update their PHP version?

I have 1and1 and I don't think they'll be fast about updating anything.


Ryanmr, you should be able to request your host to update,
or at least ask how long it will take for them to update,
big hosting companies will make an update soon if, they already
haven't done it, like godaddy.com i think is already done with updates..

infonama 06-03-2010 04:51 AM

i am having the same problem.


All times are GMT. The time now is 12:37 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0