 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
07-01-2009, 11:22 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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 )
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
07-01-2009, 11:49 PM
|
#2 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Quote:
Originally Posted by Orc
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..
|
|
|
|
07-02-2009, 04:43 AM
|
#3 (permalink)
|
|
The Contributor
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
|
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.
|
|
|
|
07-02-2009, 09:25 AM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by codefreek
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..
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
07-02-2009, 09:28 AM
|
#5 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by ryanmr
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
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
07-02-2009, 10:44 AM
|
#6 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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', '');
__________________
|
|
|
|
|
The Following 2 Users Say Thank You to Tanax For This Useful Post:
|
|
07-02-2009, 11:22 AM
|
#7 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
|
I think use will also be used to specify which namespaces you're wanting to use. I like that feature in anonymous functions!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
07-02-2009, 03:00 PM
|
#8 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
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)
__________________
|
|
|
|
The Following User Says Thank You to Kalle For This Useful Post:
|
|
07-02-2009, 03:09 PM
|
#9 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Quote:
Originally Posted by ryanmr
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..
|
|
|
|
06-03-2010, 04:51 AM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Mar 2010
Posts: 8
Thanks: 0
|
i am having the same problem.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|