Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Since it's the holiday season now and everybody's making wishes, I wonder - which language features you would wish PHP would have added? I am interested in some practical suggestions/wishes for the language. By practical I mean:

  1. Something that can be practically done (not: "I wish PHP would guess what my code means and fix bugs for me" or "I wish any code would execute under 5ms")
  2. Something that doesn't require changing PHP into another language (not: "I wish they'd drop $ signs and use space instead of braces" or "I wish PHP were compiled, statically typed and had # in it's name")
  3. Something that would not require breaking all the existing code (not: "Let's rename 500 functions and change parameter order for them")
  4. Something that does change the language or some interesting aspect of it (not: "I wish there was extension to support for XYZ protocol" or "I wish bug #12345 were finally fixed")
  5. Something that is more than a rant (not: "I wish PHP wouldn't suck so badly")

Anybody has any good wishes?

Mod edit: Stanislav Malyshev is a core PHP developer.

share|improve this question
9  
@Stan: Much as you'd like to avoid that kind of comment, you're going to get it anyway. The problems people have with PHP are largely in the categories of things you're ruling out in your post. [...] – Fishtoaster Dec 19 '10 at 0:56
23  
[...] You're saying "How can we improve the experience of getting hit in the face without actually not hitting you in the face?" I mean, yes, getting free coffee while we're being hit in the face might be nice, it doesn't really address a lot of the underlying problems with, well, being hit in the face. So, while I hope you get some useful answers here (as there already appear to be), don't be surprised by unproductive ones. – Fishtoaster Dec 19 '10 at 0:59
5  
@Fishtoaster: if PHP associates with being hit in the face for you, by all means keep away from it. You are most definitely not interested in improving it. It so happens though there are people who are. This topic is for them, not for you. I'm sure this site has a lot of topics for you too, this is just not one of them. – StasM Dec 19 '10 at 7:57
5  
I'm using getting hit in the face as an example- a situation where superficial improvements aren't that important; when most people's problems are with the underlying thing. I'm not even knocking your attempt to get suggestions for those superficial improvements- I'm just pointing out why you're likely to get a few unhelpful answers, given the situation. – Fishtoaster Dec 19 '10 at 8:23
6  
@Fishtoaster: Not everyone, surprisingly, hates PHP - I have always liked it. Very flexible and quick (to code). – Orbling Dec 19 '10 at 14:52
show 8 more comments

closed as not constructive by Thomas Owens, Walter, Mark Trapp Sep 24 '11 at 7:47

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

77 Answers

1 2 3

make a point in php process execution which stops output to browser. i.e. flush, but with guarantee browser does not waiting for any other output. In that way one could make expensive logging queries or whatever after response was sent.

share|improve this answer

In most comments i see that people want php to be like ruby.

Santa Claus please DON'T do that!

PHP is ok for me, the way it is.

share|improve this answer

few things that would make my day

  • Sane naming conventions for built-in functions.
  • Type hints for strings and numerics
  • Return type hinting
  • E_STRICT on by default
  • Traits, mixins, & multiple inheritance
  • Everything is an object (i.e. ruby-like pureness)
  • Add :: support to namespaces
  • Better windows support
  • Testing out of the box
  • Better documentation for the underworkings of exec()
  • Redesign of php.net with live-search
  • Xdebug like functionality out of the box
  • Improvement of PEAR portability - users of ruby gems should know
share|improve this answer
  1. Immutable value objects
  2. Anonymous classes and/or classes as objects
  3. Builtin object equivalent to string data type (mentioned earlier)
  4. Annotations or Python-like decorators
  5. Singleton objects like in Scala
  6. Default errors as exceptions (mentioned earlier)
  7. UTF8 support
  8. Removal of global etc
  9. Unified access principle - one way to call object methods and manipulating properites (see Scala)
share|improve this answer
  • UTF-8 support
  • Make the language fully OO, borrowing the Ruby and Python concept that everything is an object. I kinda liked the autoboxing rfc. However it gives way too much freedom to the developers which is not that good. But with some limitations it could be a nice addition to the language evolution.

$x = array(5, 60, 50, 50); $x->map(function($i) { return $i * 2; })->push(10);

$p = "some string"; $q = $p->substring(0, 10);

etc.

In my oppinion this can be done without breaking the current global functions. However, most of them will become useless and could be deprecated over time.

  • Short notation for arrays would be nice, but it's not critical for the language.
share|improve this answer
show 2 more comments

It would be nice to be able to use a class that extends iterable in a foreach loop, where you pass a reference to the loop:

foreach(&$myclass as $me) {
  echo $me;
}

I haven't spent much time looking into why that doesn't currently work, perhaps it's related to how iterables work, I haven't investigated much more than just noticing that it doesn't work.

share|improve this answer
show 1 more comment

Being able to initialize objects directly and without having to cast an array to an object:

$my_object = object('property'=>'value','property2'=>'value2', [...]);

Instead of using "new stdClass()" and then setting property by property in separate lines.

share|improve this answer

I need some erlang features in php:

  • hot code loading
  • atoms
  • pattern matching (include name of functions, matching statement like: case of)

Working with bytecode: saving, loading, removing and so on...

Flexible embedding system

share|improve this answer

Expose zval reference count. (Yeah, we could use xdebug_debug_zval, but enabling Xdebug on a live site, ick.) Use case: active record object store - you have models which correspond to external resources (like database rows), and are responsible for modifying those resources. Having two separate object representations for the same resource would be bad (data loss due to conflicting writes and so on), so we need some sort of cache which can return the model for the requested resource if it has been loaded already. That would kill garbage collection, however: a last reference to the model object would always remain in the cache, so iterating through a large set of resources like a big DB query or a large directory would quickly eat up memory. This could be avoided if the object store could be check whether there is only a single reference to the stored object (the store itself) and destroy it if that is the case.

share|improve this answer
1  
@StasM: how about weak references? That is a feature supported by a lot of languages, seems easy to implement, and makes it trivial to create an object store whose contents can be garbage collected. – Tgr Apr 20 '11 at 10:11
show 1 more comment

My number one feature would be

operators overloading

In my opinion, one recurring feature asked for here, namely native types as objects, can be fixed by creating your own wrapper classes. I have developed for my projects an arrayData object, a stringData object, an intData object, and so on... This solves:

  • Strong typing: since those are custom classes, they can be enforced
  • Type overloading: I can add whatever methods I need to my arrayData class
  • Type conversion: each of my classes has ->toArray(), ->toInt(), ->__toString() methods, and so on
  • html escaping in templates (strings are userStringData class that extends stringData class).
  • values are always passed by reference unless instructed otherwise
  • creating an intData('string') throws an exception.
  • etc (the list of benefits is still extremely long)

Imho, this is more beneficial than native types as objects, since you can have the exact number of methods you need, and call them to your liking.

But what I miss oh so much is the ability to use native operators on my objects. I am able to use the [] operator thanks to arrayAccess. But I can't use "+", "-", etc. If I could, then I could do stringData + stringData (equivalent to $string.$string), or stringData-stringData (equivalent to str_replace($str,'', $string)), or compare my types with ">" and "<="...
My current implementation uses $intData->add($n), $intData->substract($n), and so on. Cumbersome, specially in functions where you could expect either a native int or an intData object. Which means I have to check with instanceOf inside each function.

In other words, although my classes are ready, optimized and nice, until I can overload operators, they are not much more than a proof of concept. Using them in an actual project is annoying.

share|improve this answer
show 2 more comments

Faster function calling

We have call_user_func($f,$a1,$aN), but it's been superseded with $f($a1,$aN). However, there's no such thing for call_user_func_array($f,$args).

My proposal is to create a specific language syntax for this, such as $f{$args}. The reason everyone should stay a mile away from call_user_func* is that they're extremely slow and ugly looking in the sense that there are better alternatives.

Object decleration syntax

Right now, to create an object on the fly, you need: (object)array('prop'=>'value');. By convention, we should also have object('prop'=>'value');. Also, short syntaxes would be handy, similar to JSON.

A be-all-end-all magic method for types

Right now, we have __toString(), and many suggested __toInt/__toFloat/etc. My advice would be to implement __toType() or __typecast(), which as a first parameter, the desired data type is passed, eg:

class Test {
    public function __toType($type){
        switch($type){
            case 'integer':
                return (int)$this->age;
            case 'string':
                return $this->age.' years';
            default:
                throw new EUnsupportedTypeException();
        }
    }
}

If we wanted to be more specific, we could add another argument after $type, namely $class. So you can: if($class=='person')return new Person($this->age);

Specifying Data Type in foreach

Currently, you can specify the data type of a PHP function argument, like so:

public function say_hello_to(UserClass $user){
    $this->say('Hello, '.$user->name.'!');
}

It would be great to do this in a foreach as well:

public function on_enter_office(){
    foreach($users as UserClass $user) // <- See UserClass here?
        $user>say_hello_to($this);
}

The current "fix" is using a closure, like so:

public function on_enter_office(){
    $users->each(function(UserClass $user){
        $user>say_hello_to($this);
    });
}

The fix takes more resources, more writing and messes the scope, hence why a native solution will make it easier, cleaner and probably faster than the current fix.

Conditional Defines

This probably won't be a useful feature for many people, but it is a great way to keep the running code at a minimum even when it is compatible with old systems, making execution faster. Consider the following code:

if(!function_exists('json_encode')){ function json_encode($value, $options=0){ // legacy code } }

  • The // legacy code section is still parsed, hence any errors in there will cause PHP to quit.
  • Parsing it also makes PHP slower, even if it doesn't need it at all.
  • The code is not intuitive to developers
  • Any IDE parsing engines will get confused since they ignore if statements and end up listing the function twice.

The fix? Conditional compilation:

#if PHP_VERSION<5.2
function json_encode($value, $options=0){
    // legacy code
}
#endif
share|improve this answer

Dot-notation for OO. I can't stand -> syntax.

share|improve this answer
show 2 more comments

The vast majority of these wishes seem to be about 'how' PHP does stuff, with various people wanting either modifications to the language itself or changes in the way the makers have implemented it.

What I'd like to know is: what tasks would you never, or at least very rarely, attempt with PHP? Okay, no one in their right mind would attempt to write a C compiler in PHP; that's swinging the pendulum a bit far. But what tasks between here and there are a challenge for PHP, that really push the PHP envelope?

share|improve this answer
show 2 more comments

destroy($object) - nullify object and all references (it can be a PITA chasing some object through all places where it is referenced). Optionnaly, access to list of all references to given object / var (maybe some extension of Reflection?)

share|improve this answer

Static inheritance is a much needed feature.

For instance, right now, I can do this:

class First
{
    static function class()
    {
        echo __CLASS__;
    }
}

class Second extends First {}

Second::class();

Will output "First."

This completely eliminates the possibility for doing neat static methods a la Rails' ActiveRecord-style 'ClassName.find(x)'

edit: This reminds me of PHP's weird concept of "$this" where an object method really only HAS to be called in an object context if it uses $this... though I realize that as far as code design, you probably shouldn't be defining a lot of instance methods that don't affect or rely on the state of the instance.

share|improve this answer
1  
See php.net/get_called_class for that. It's been available since PHP 5.3. – janmoesen Dec 27 '10 at 12:12
1  
Look into late static binding – dukeofgaming Dec 31 '10 at 19:35
show 1 more comment

BEING ABLE TO REDECLARE FUNCTIONS!

In python you can redeclare functions, because they act just like variables.

mysql_query=debug_mysql_query

and being able to point to a single function within an object:

my_function=obj("passed to the constructor").some_func

REMOVE GOTO

I can't believe they added this train wreak. This quite possibly the worst addition to PHP.

On a side note they are removing magic_quotes_gpc and register_globals Which is a wonderful thing.

share|improve this answer
show 5 more comments

Have an autoload callback for use statements.

share|improve this answer
1 2 3

Not the answer you're looking for? Browse other questions tagged or ask your own question.