What for PHP6?
Fabien Potencier
June 07, 2009
PHP 5.3 is just around the corner with a lot of great new features. However, even if I'm really excited about this new release, I won't make yet another PHP 5.3 feature list; I will rather look at the future of PHP. PHP core developers met at php|tek and discussed the future of PHP. And it is really great to see that they plan lots of wonderful features; let's set aside the Unicode stuff.
They published some notes from the meeting, and here is my personal list for things I find really interesting:
Add
__cast()magic method that will be called for all casts. If the__toString()method is there it will get used for string types first.Consider making a "callable" type.
Make
ArrayObjectandArrayAccessaccepted everywhere regular arrays are.Add traits support.
Add type hinted return values, scalar type hints.
Make function call chaining possible (
f()()iff()returns a function), and array dereferencing (f()[0]).C#-style properties with getters/setters:
class Foo { public $bar getter { return $this->bar; } setter { $this->bar = strtolower($value); } ; }




Discussion
Whilst most of your points are a huge negative to PHP as a language and to the style of language it is, don't you think that your features you want just add more complexity?
I do... We have enough bloat as it is, what with GOTO, for example.
Those folks behind PHP6 have managed to completely fu*k it up with the namespace separator, so yer... Why not lets screw it up altogether now?
Sorry, but I feel strongly when it comes to PHP and I just wish that full time PHP developers would leave things as they are.
If you want something more, then move to Python, or even Java as a last resort.
Fabien I agree with you, and think this list looks great!
These features will make PHP even more powerful and better to use. I love the idea of more type hinting and last but not least the C#-style getter/setter will make your PHP-code probably much better to maintain.
if you don't want to change, keep building your personal home page in 1.0... else evolve.
If guys at PHP would leave things as they are, we'd still be using PHP/FI. Evolving a language is important.
I'm really <em>really</em> excited for some of the features, like array dereferencing, anonymous functions, function chaining, ... Yes, PHP supports anonymous functions, but the current implementation is more of a joke.
So, I'd like to see and use these features in PHP. Changing the language just for that is, sorry to say, absurd. I'm already using several languages for different purposes, and PHP is simply the best for web front-end development. Why would I want to use another, less effective tool, if I can use my tool improved?
It seems to me that the PHP community is just trying as hard as possible to implement any new cool concept in the core language and this is wrong.
Why not add even more? Features likes: annotations, partial function applications, tail recursion, import ... as ... , decorators, generators, context managers, class invariants, support for design by contract and maybe even transactional memory ???
Sounds nice? Maybe. But if you add a bunch of unexperienced programmers to this mix you will end-up with a lot of hard to maintain software.
C#-style properties with getters/setters is also a thing i would like to see.
"[]" instead of array() is something i can't understand why do not just have.
if(f()[0] == true){ $var = f()[1]; }
On the other hand i would like the ternary operator shortcut to act more intelligent.
echo ($var1 == 10) ? : '' ; // outputs 1
I love the progress of the language but i guess php will always come with a bump and a bruise.
And yes some idiots will abuse new syntax. But lets not get to hung up on that, just do like me and prevent to work with idiots;-)
All in all I can't wait for PHP6 to come out.
PHP is a free-style writing language but that doesn't mean you wont take advantages of this. Scalar hint and return hint is a MUST because I'm tired of writing @return just to get more inteligent auto complete ;)
Function chaining and array dereferencing is not a must for me.. but it is another feature of the language. You see bad code everywhere.
Those getters and setters look good and that is a feature that will make people write better code.
I have evolved as you put it, from developing PHP as procedural three, maybe four years ago, to now developing fully OO with best practices, etc today.
From my view, it's not the language that so much that has to evolve, but may peoples skills? If you keep changing PHP at every major redevelopment, then people for the most part, are simply unable to keep up with additional changes.
We have PHP6 because we need to have PHP6, because we require namespaces for better code, we require unicode support to be able to compete with other languages, etc.
We simply don't need a "(death) wish list" of features to bloat a perfectly good language.
> It seems to me that the PHP community is just trying
> as hard as possible to implement any new cool concept
> in the core language and this is wrong.
I concur; Ruby it's self is a beautiful language, but God RoR made a complete ar*e of it, didn't it? If we have bloat, then PHP is surely going to go the same way.
That is what has happened to the Zend Framework, it's bloated beyond comparison - class methods to accept just about any type of parameter, where as proper design would only really allow one type through...
You leave it to the type to manage the data, not a -beep- class method :(
Anyways, respect to all.
If that is the opinion of many, then that in my view is only a short term measure to a far wider problem... If the industry is short of PHP developers, then that is a skills gap issue, not a language/platform issue.
Maybe if we put our resources to better use elsewhere, then things would sooner sort themselves out ;)
- not for the development language to allow new/improved coding styles, to gain benefit from new insights.
- and not for the developers, to keep them actively thinking about the language and how new features could be used to improve their coding style and probably just as important: their applications.
Besides, it doesn't look like any of the proposed features is going to be required for you to use.
For now, I can only say that I really like the proposals
But really powerful toys do not belong into amateurs hands.
Thats why nobody ever tried to make Java or C++ easy to learn or understand.
Unreadable code can be produced with the least complex language and even most complex features can be used wisely and with speaking function/variable/method/class-names such that the code is very well readable.
IMHO the only way to keep yourself from trashy code is to follow Marijn and prevent working with idiots. There is no problem with complex language features. There is a problem with PHP attracting script kiddies thinking of themselves as gods of source-code just because PHP makes it easy to archive first results. Thus people never understanding the ideas and concepts of software developing get chances for programmers jobs, even if they fail and switch to something else, they leave much of crap behind. Already saw PHP developers who switched to jobs with J2EE or Python just to get rid of that "Look at me, I can build a forum with PHP"-idiots and the code they leave behind.
PHP6 seems a little like an bomb with a simple big red button and a "3 steps to detonation" instructions label laying around on a playground.
Unfortunately I do not have a clue how to make PHP less amateur attracting.
To those who believe less complexity is their holy grail I recommend Marty Hall's Evaluation of servlets vs. other technologies from his book "Core Servlets and JavaServer Pages" and if you do not agree with him after reading go Assembler.
and i hope it will walk this way for the next ten years.
The skills you need to start working with php are low, thats good, because a lot of talented programmers start their career in web development by using php, and stick around with it, thats why php has this great amount of flexible, free and easy to use libraries.
Everyone of us has written awkward code at the beginning, independent from the coding language. So i can´t understand things like "Unfortunately I do not have a clue how to make PHP less amateur attracting. ".
Well good, that you don´t have a clue, because nobody wants PHP to be less attracting, PHP has and will be come more attracting. It kind of grows with the skills and needs of its community. And thats a really good thing.
PHP is flexible, easy to learn and you can "grow" as an developer with the language. (I guess, there are a lot of other advantages and yes, also disadvantages, but lets keep it that simple)
So, back to the main topic.
I`d like to mention some thing, i found really usefull, after reading the notes:
!. Implement reflection cache (No need for userland workarounds anymore)
I agree with all the topics Fabien mentioned above:
1. Getters and setters (because we all use them by now, and if we don`t we should - Great for upcoming topics like dependency injection)
2. Scalar Type Hints (Make code easier to read and less error prone)
3. Add traits support (Traits are something thats been really underrated by now, could help us write less and less error prone code)
4. __cast() Method (So we get rid of all the toArray() Methods, resulting from this, we might get "better" code in our views and templates.
5. Make function call chaining possible… (Mhh, could be a good thing, but maybe makes code harder to read, understand)
With the Traits topic coming up again, we may also should discust, some AOP features built into the core.
Flow3 uses this as a userland implementation, but makes use of eval() to compile them at runtime and then is in need to store compiled class files.
Might be good to have such an higher level feature built right into the core.
Another topic, which i have special place in my heart for, are Annotations. I use em, i love em and 50% of me want to have them as a language feature, the other 50% don´t because you can define them in userland pretty well (take a look at Addendum, if you´re interested in)
This post is much longer than i thought to after started writing the first sentence.
Thank you if you already stick around by now.
> Unfortunately I do not have a clue how to make PHP
> less amateur attracting.
That is a problem with PHP; something that has followed our beloved language for years now and it just doesn't help the industry as it's a plague upon it's self.
But in my opinion, you can only really start taking the trash out through education and asserting the needs to (rather?) enforce better skills, so we can kick bad habits.
Frameworks are a start, unfortunately there are (now) bad frameworks, so the trash hasn't been taken out, merely swept under the carpet :(
Lack of understanding, skill and investment in developing skills is partly the problem as I see it, as people are paid to push out code by a deadline, rather than to develop code to a given, higher standard.
It is okay to add additional features for those of us who would know better when and when not to use them, but those who are none the wiser would just abuse those features... and simply add to the trash :(
That is where I have a problem I suppose, so my thoughts are we need to slow down.
C#-style properties with getters/setters:
However these getter/setters has existed in Borland Delphi/C++ Builder since 1997. The main architect of C# was the same guy who created Delphi way back then.
You should really be saying ...
"Delphi-style properties with getters/setters:"
I have written a post about PHP/Java/JS relationships:
""
PHP 5 is close to Java 1.4 and maybe even more much closer to JavaScript!
http://www.jroller.com/dmdevito/entry/php_5_is_close_to
""
PHP has followed quite a long trip to become closer and closer to Java.
PHP 5 is somewhat Java 1.4:
+ function (but static methods in Java are very close to PHP function)
+ closure
+ interpreted mode (but Java+JavaRebel could make the day too).
- unicode
- performance
If closure are going to be added to Java, then PHP and Java would be quite very close.
While JS has function+closure, one could say PHP sits between Java and JS.
In fact, AFAIK, let's see another Java cousin: C#.
Well, C# has function+closure.
So, as PHP is evolving , why PHP community changes its language instead of adopting C# ?
So, the point is I don't heard the speech beyond PHP rational evolution, I just see the will to adopt nice features other languages have.
Well, you know what ? JS is moving to the server-side, just read another of my posts:
""
A false piece of news may become real as Java and JS move closer
http://www.jroller.com/dmdevito/entry/a_false_piece_of_news
""
So, the battle between all these languages are still going to evolve...
Javascript has low features (don't have classes, namespaces, inheritance ecc...)
but his flexibiliy permits of write fluent interface like jQuery or Prototype.
Try to see also the library Functional.js of Oliver Stelle for see what is possible to do with a language flexible like javascript.
Another method for make clean code readibility is the use of dsl.
In ruby the dsl are too much used, and the elengange and flexibility of language permit of have feature near to natural language.
See this example in ruby:
result=search.for :flight do
from :milan
to : rome
at :date => "12/09/2009", :time => '04:00'
with_carriers [:az,:lh]
end
Is possible to make a nice work also in javascript using the chainability:
var result=search({for:'flight'})
.from('milan')
.to('rome')
.at({date:"12/09/2009", time: '04:00'})
.with_carriers(['az','lh'])
.get();
see also the library fluently of Oliver Steele for nice examples of this method of programing.
Or using also the with is possible to make code mode readable in javascript:
var result=search({for:'flight'},function(search) {
with(search) {
from('milan');
to('rome');
at({date:"12/09/2009", time: '04:00'});
with_carriers(['az','lh']);
}
});