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 ArrayObject and ArrayAccess accepted everywhere regular arrays are.

  • Add traits support.

  • Add type hinted return values, scalar type hints.

  • Make function call chaining possible (f()() if f() 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); }


gravatar Les  — June 07, 2009 23:30   #1
My comments are not going to be gracious for your opinion but I strongly disagree with your thoughts.

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.
gravatar lvanderree  — June 07, 2009 23:43   #2
@Les I can't say I agree with you

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.
gravatar gasper_k  — June 08, 2009 00:43   #3
Les, I also disagree.

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?
gravatar Andrei  — June 08, 2009 01:22   #4
This is silly. PHP already implements concepts from almost all programming languages. There is no consistency in the implementation and this makes applications hard to maintain because each programmer will write code in his own style.

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.
gravatar Herman Radtke  — June 08, 2009 06:27   #5
I agree with Les. When people start realizing that writing code is the least amount of time spent in an application life-cycle, they will stop all these coding "shortcuts". Things like function chaining and array dereferencing may be useful in some circumstances, but they will often lead to writing hard to read code.
gravatar Mikael  — June 08, 2009 09:33   #6
I like that traits are considered, but why not full multiple inheritance ?
gravatar uli  — June 08, 2009 09:45   #7
in my opinion function chaining and array dereferencing are good things when you don't abuse these features and use them too heavy. A little getMyArr()[5] would be nice, saves time and can contribute to a better code overview.

C#-style properties with getters/setters is also a thing i would like to see.
gravatar krob  — June 08, 2009 10:38   #8
I'm of the belief that if you write code, your first priority is to make it easy to understand. Just because you may understand your code for the first for days/weeks/months doesn't mean if you go back to it 4-5 months later everything will be the same. During that time span you may start using a different language, or stop coding or something--brain damage ? Who knows and your complex optimised code will start to stink when you say to yourself, wtf was i thinking ?
gravatar Harel  — June 08, 2009 14:03   #9
When someone says PHP I immediately think of the word 'inconsistent' . I agree with Andrei.
gravatar anonymous  — June 08, 2009 14:30   #10
I think we should also propose stuff to throw away, and make a big list of that stuff.

"[]" instead of array() is something i can't understand why do not just have.
gravatar david  — June 08, 2009 14:44   #11
I have to agree with the ones who think shortcuts are going to be abused, just like the goto label. I already foresee to read code like

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.
gravatar Marijn Huizendveld  — June 08, 2009 15:27   #12
Finally, decent getters, setters and even return values:-) I hope the getters, setters and return types are supported at interface level as well...?

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.
gravatar MX  — June 08, 2009 16:44   #13
You can also abuse in Java, C++, C.. and write awful code.
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.
gravatar Les  — June 08, 2009 16:56   #14
> keep building your personal home page in 1.0...

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.
gravatar Les  — June 08, 2009 17:05   #15
Sorry for double posting, but do people believe that we need these new features to attract new developers to PHP?

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 ;)
gravatar lvanderree  — June 08, 2009 21:21   #16
Les. I did not meant to disrespect you, but I don't see what is wrong with some evolution:

- 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
gravatar madman  — June 08, 2009 21:48   #17
Complex features are needed so solve complex problems.
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.
gravatar Sebastian G.  — June 09, 2009 10:38   #18
PHP has gone a long way in the last ten years,
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.
gravatar Les  — June 09, 2009 19:02   #19
Hello again,

> 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.
gravatar john  — June 09, 2009 19:06   #20
Is PHP6 ever going to come out? I've been hearing about PHP6 for 5 years now...
gravatar Colin B Maharaj  — June 10, 2009 03:19   #21
You say .....
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:"
gravatar Dominique De Vito  — June 13, 2009 11:17   #22
As a Java programmer, I feel a bit difficult to see about PHP advantages, what are the features that make it specific, beyond its community.

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!

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

So, the battle between all these languages are still going to evolve...
gravatar Thijs  — June 15, 2009 14:51   #23
I would be nicer if they implement the Objective-C-style (Leopard/iPhone development) getter/setter methods using a @synthesize-like directive which is introduced in Obj-C 2.0 rather than the more verbose C#-style.
gravatar Dude Dude  — June 17, 2009 01:15   #24
gravatar sasuke  — June 28, 2009 14:47   #25
i believe that the importante of writing beauty code depends too much from the flexiblity of language.

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]

Is possible to make a nice work also in javascript using the chainability:

var result=search({for:'flight'})
.at({date:"12/09/2009", time: '04:00'})

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) {
at({date:"12/09/2009", time: '04:00'});