DaemonForums  

Go Back   DaemonForums > DaemonForums.org > News

News News regarding BSD and related.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 14th November 2011
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default PHP 5.4 approaches with first release candidate

From http://h-online.com/-1378615

Quote:
The PHP development team has announced the arrival of the first release candidate (RC1) of version 5.4 of its PHP scripting language. The next major release of PHP introduces new language features, such as traits for better code reuse, an integrated web server and a short syntax for arrays. Changes since the second beta include the addition of the ability to access a class member when creating the class and making a conversion from an array to a string produce a notice.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #2   (View Single Post)  
Old 14th November 2011
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

Quote:
Changed silent conversion of array to string to produce a notice.
Finally!

Printing "Array" is not useful at all and is *never* what you want. Instead, do what every other programming language does: Just print contents (i.e. what print_r() does), so we're not quite there yet. In any case, a warning is a lot better.

We also *finally* get the "short" array syntax, which is also what pretty much every other language does. This feature only took about 4 years to implement in PHP (literally, this has been in the works for ages).
You can now use:
$arr = ['key' => 'value'];
instead of:
$arr = array('key' => 'value');

Both "indexed arrays" and "associative arrays" use the same syntax, which is a bit of a shame IMHO. They're really separate data structures with different properties, at least, they are in most languages, maybe there is no difference in PHP's implementation (I would not be surprised...)?

Ofcourse, you still have to use array_*() function which makes the saving minimal, not only are they pretty long to type, they're not always easy to work with since you never know which order the specify the arguments. For example, it's array_map($callback, $array) and array_walk_recursive ($array, $callback) just to name one example. It's just really really silly.

One more piece of good(-ish) news is that you can now use:
$var = fun()['2'];

This is probably something of a quick syntax hack, and certainly not the same "everything's an object" model Python or Ruby sport, but it saves a line of typing.

I wonder if it allows:
$vars = fun()['2']['4'];
or
$vars = fun2(fun()['2'])['3'];

One new feature that's also interesting is proper upload tracking through sessions, you can access $_SESSION['upload_progress'] and get the upload start time, file size, filename, etc. Very useful for creating progress bars and the like: Something that was previously semi-impossible, all the existing "solutions" only worked with Apache/mod_php and/or were not reliable in large numbers.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #3   (View Single Post)  
Old 14th November 2011
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

I still wonder why PHP still cannot tag data retrieved from the net,as tainted, like in Perl. See http://en.wikipedia.org/wiki/Taint_checking
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #4   (View Single Post)  
Old 15th November 2011
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

Right, that sounds pretty useful.

At least in Python and Javascript, and I suspect in Ruby and Perl, this would actually be fairly easy to implement, for example in Python you can use:

Code:
>>> class Myint(int):
...   myprop = 0
... 
>>> var = MyInt(42)
>>> var
42
>>> var.myprop
0
>>> var.myprop = 1
>>> var
42
>>> var.myprop
1
>>>
You just subclass the int type (it's just an object) and add a new property: You can then use this property to flag if the variable has been escaped, you would only need a "custom" escape() and query() function which sets/checks it. Actually, it may be best to just add an escape() method.

Anyhow, I once looked at implementing something like this in PHP. At my previous employer I had built a webshop, originally, all the prices should be displayed with VAT, but then later (naturally, after the whole thing was finished & was live ) there was a request for users to toggle prices display with or without VAT.
My original idea was similar to the above: Create a new int type, add a flag "VAT", and use that to calculate the correct price on invoices and so forth, this would provide the maximal flexibility and the least coding.
As far as I could find, there is no real way to do this in PHP

There are workarounds: you can extend the ArrayObject or StdObject, and using that you can *simulate* "custom types" and flag if the variable was escaped, but IMHO it's a lot less cleaner than the above example in Python ...

But with sufficient planning and design, you could use it to implement taint checking ... Perhaps some of the frameworks like Doctrine already do something like this (Or use a different approach?).
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #5   (View Single Post)  
Old 15th November 2011
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by Carpetsmoker View Post
One more piece of good(-ish) news is that you can now use:
$var = fun()['2'];

This is probably something of a quick syntax hack, and certainly not the same "everything's an object" model Python or Ruby sport, but it saves a line of typing.

I wonder if it allows:
$vars = fun()['2']['4'];
or
$vars = fun2(fun()['2'])['3'];
Similar syntatic sugar has been in Perl since dirt was formed. In fact, I have been asked variants of this question in job interviews just to gauge much experience I had with the language. For example, if constructing today's date is the desired purpose:
Code:
#!/bin/env perl
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
printf "%d-$mon-$mday\n", $year + 1900;
...can be reduced to the following which eliminates a lot of extraneous clutter:
Code:
#!/bin/env perl
my ($year, $mon, $mday) = (localtime time)[5,4,3];
printf "%d-$mon-$mday\n", $year + 1900;
FWIW.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
jQuery 1.5 nears with release candidate J65nko News 1 1st February 2011 06:34 PM
FreeBSD PC-BSD 8.2 approaches with first release candidate J65nko News 0 11th January 2011 05:14 PM
Ruby 1.9.2 gets a second release candidate J65nko News 0 12th July 2010 05:57 PM
Ruby 1.9.2 approaches with release candidate J65nko News 2 9th July 2010 06:13 PM
Python 2.6.5 gets a release candidate J65nko News 0 3rd March 2010 12:39 AM


All times are GMT. The time now is 10:36 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick