Page 1 of 1

Developing PHP 6 Applications?

Posted: Tue Jun 23, 2009 8:25 am
by kaisellgren
Hello,

Lately I've been looking into PHP 6 and what it has to offer. I am trying to find out things that a developer has to understand in order to support PHP 6. So far I have noticed that:

1) Unicode aware functions
In PHP 6, all functions are Unicode aware. This can lead to problems when you use functions in non-Unicode way like:

Code: Select all

echo strlen(file_get_contents('binary_file.bin')); // 32
echo filesize('binary_file.bin'); // 40
The function strlen() is Unicode aware, and will return the number of characters rather than the number of bytes. It's cool that you can finally count the string length with it, but people who have used it for other purposes like byte counting, their code will miserably fail. [1]

2) Character code point access

Code: Select all

$a = "??";
echo $a[1]; // will output ? - not the byte
which could create problems with applications that deal with bytes. [1]

[1] I am not entirely sure, but is there a way to disable Unicode semantics? Also, as far as I know - the encoding used for functions is determined by the script encoding? Does this mean that if index.php is UTF-8 which includes binaryProcessor.php which is ASCII - the functions in binaryProcessor.php will not use UTF-8?

3) Namespaces
It seems that the separator for namespaces is going to be the stupid \ character. So, a class like

Code: Select all

class MyProgram
{
 static function MyGlobalFunction() {}
}
Would be accessed like

Code: Select all

MyProgram::MyGlobalFunction();
It would be great if we could just replace the class file when switching to PHP 6,

Code: Select all

namespace MyProgram;
function MyGlobalFunction();
But thanks to the great namespace delimiter \, we must recode everything in our whole application:

Code: Select all

MyProgram\MyGlobalFunction();
All third-party extensions and code would need to adapt to this...

Do you think that it is impossible to program a complex program that works in both PHP 5 and 6? What kind of things are needed to be handled if you are to support both of them? If you support them both, what features of PHP 6 you can't use? Or should I and other developers just wait until PHP 6 is mainstream and switch to it = recode everything and ask potential 3rd party module writers to adapt, too?

All thoughts welcome. :)

Ah, it seems that you can easily define the encoding to use in a PHP file:

Code: Select all

<?php
declare(encoding="ASCII");
echo strlen("?"); // 3 (bytes)
declare(encoding="UTF-8");
echo strlen("?"); // 1 (character)
?>
But the question is, what can you do if PHP 5 doesn't recognize the declare()?

So, how much do you care about PHP 6? Will you only move to PHP 6 and drop the support for PHP 5 when you see the appropriate time to do so?

One thing that I really would like to know is that when PHP 6 is out?! Next year? 2 years? 5 years? If it takes many years for it to come out, I can stop worrying right now and just concentrate on PHP 5.

Re: Developing PHP 6 Applications?

Posted: Tue Jun 23, 2009 8:44 am
by Weirdan
But the question is, what can you do if PHP 5 doesn't recognize the declare()?
Unrecognized declares are ignored:

Code: Select all

 
weirdan@virtual-debian:/home/sam/trunk$ php -r 'declare(encoding="w"); var_dump("here");'
string(4) "here"
 

Re: Developing PHP 6 Applications?

Posted: Wed Jun 24, 2009 7:18 am
by kaisellgren
I just compiled PHP 5.3 RC5 on Win 7 x64 (what a pain...) and god I am loving namespaces :)

declare() indeed does ignore encoding="" in PHP <= 5.3

This is all great so far, I wonder if you can specify the encoding in the function parameters like strlen(...,'UTF_8') in PHP 6? There does not seem to be any information in the manual.

Reason for this is (PHP 6):

Code: Select all

<?php
 
declare(encoding='UTF-8');
 
echo "Length of the name: ". strlen($name); // unicode supported
 
// but now I want to count bytes...
echo "Size of user supplied data: ". strlen($udata,'ASCII'); // I doubt this is going to work like this, so, any recommendations?
 
?>
If I need to count both bytes and characters, I wonder how am I going to accomplish that in PHP 6. Would I just declare() again? Sounds awkward. :P

Or do I need to use mb_strlen() for change -.-

Re: Developing PHP 6 Applications?

Posted: Wed Jun 24, 2009 9:27 am
by Weirdan
If I need to count both bytes and characters, I wonder how am I going to accomplish that in PHP 6. Would I just declare() again? Sounds awkward.
I guess it should be:

Code: Select all

var_dump(strlen((binary) $name));

Re: Developing PHP 6 Applications?

Posted: Wed Jun 24, 2009 9:51 am
by kaisellgren
Ah, that would make sense. Thanks.