For most who know me know, PHP is one of my favorite web development technologies. The problem though simply lies in the advancement and growth of this technology. Recently I have been hard at work trying to finish up a few major source packages and one application – but I have recently found myself thinking about how much better and more “grown” up PHP would be if it had a few things that other technologies such as C++ and C# have. Below is a list of the top five language features I would love to see implemented and changed within this technology.

1. Operator Overloading

The first thing that I wish PHP had and or would implement in the very near future is the ability to overload operators. So what does this exactly mean? Well, since PHP has now for quite sometime had an Object Oriented model implemented in the language, the implementation of operator overloading in my personal opinion is almost a necessary step to gain more momentum in growth and large adoption of this technology into the market share which other technologies such as C# hold. With the ability to overload operators we (PHP developers) would now be able to use a specific case of polymorphism in which operators like +, =, -, *, (), ->, [], etc. have different implementations depending on the types used within the specified expression.

Currently there is a PECL package which allows the ability to overload operators called “operator“. The problem with the use of this package is that it is not native and many “shared” web hosting environments will not install additional packages and or extensions onto the shared servers if the extension is not used by the mass population of the industry.

At the moment, I can see one problem with this feature if it was introduced into PHP as native language feature and that is the incorrect use of operator overloading by inexperienced developers.

2. Method and function Overloading

Method and function overloading is a feature found in more grown up programming languages such as C++, C#, Java, etc. which essentially provides the ability to have several methods or functions with the same name just different parameter lists and or return types. An example of this in C++ looks like this:

class MyClass
{
private:
    std::string _str;
    unsigned int _uint;

public:
    MyClass()
    {
        /* default constructor */
    }

    MyClass(std::string str)
    {
        this->_str = str;
    }

    MyClass(unsigned int uint)
    {
        this->_uint = uint;
    }
};

The above example shows the concept of method overloading with three different class constructors for the class MyClass.

I would like to see this feature be implemented into PHP for the fact that it would provide us developers with the ability to write cleaner methods along with the ability to reduce the amount of parameter checking for basic methods or functions.

3. Advancement of Namespaces

Alright, are you absolutely freaking serious? Recently (well not so recent, but recent enough) PHP 5.3 was released. One of the new language features introduced in PHP 5.3 was the support for namespaces. Though it is great that PHP has no taken off it’s diapers, however there are still times that it wets the bed; metaphorically. First off, let’s see what the problems are with this new language feature:

  • The chosen namespace separator. Unlike most languages, the namespace separator in PHP is a single backslash (). The problem with this is simply that it does not follow the convention that is typically used throughout most languages and that is the use of the double colon (::) as a namespace separator. There are also problems with character escapes when used within a string – typically found in code written by beginners.
  • Another problem is that namespace declarations must be the first thing within any PHP file. Okay, this is understandable and not the biggest issue.
  • Lastly, one of the biggest problems is the inability to declare a nested namespace within another without having to declare it within its own separate file.

In my personal opinion, I believe that PHP should change the namespace separator to the double colon (::) which will follow the convention used by many if not all other languages and also introduce the ability to write nested namespaces. Here is an example of what a nested namespace could look like:

namespace MyParentNamespace
{
    namespace MyChildNamespace
    {
        function HelloWorld()
        {
            print 'Hello World';
        }
    }
}

And the use of the double colon convention as a namespace separator instead of a backslash would look similar to:

// This would print Hell World
MyParentNamespace::MyChildNamespace::HelloWorld();

4. Use of final keyword for class member variables

One of the available modifier keywords for classes and class methods in PHP is final. In most languages, this is usually also available to the class member variables however it is unavailable within PHP for class member variables. One of the reasons I wish to see this modifier keyword be added as an available modifier keyword for class member variables is simply for the ability of developing large class libraries which may have many derived classes. The reason for this is so that a single member variable can be used with the confidence that it would not be “overwritten” by derived classes but still accessible.

An example of how this could look would be something similar to this:

class MyWonderClass
{
    final protected $_someWonderfulMemberVariable = 'Hello World';

    /*

    ... Method implementations ...

    */
}

Along with this and on a side note, I also wish that during declaration of member variables we (PHP developers) had the ability to assign non-scalar data types such as objects. An example of this would be:

class MyWonderClass
{
    final protected $_someWonderfulMemberVariable = new HelloWorld();

    /*

    ... Method implementations ...

    */
}

5. ‘Getters’ & ‘Setters’ similar to C#’s for class member variables

One of the most unique and interesting features I have seen in a programming language to date that I absolutely love is the way that Microsoft has implemented the ability to write a getter and setter method in C# all within a single property method definition. Here is an example:

public class MyClass
{
    private string _myString;

    public string MyString
    {
        get
        {
            return this._myString;
        }

        set
        {
            this._myString = value;
        }
    }
}

So why do I find this so fascinating? Well simply because it allows for less code to be written and with a more satisfying but generalized property method name. This also saves us from having to write two individual methods prefixing them with get and or set to denote that this method is performing this type of operation whether it is getting a value or setting a value (mutating or accessing).

Overall I believe that if the following language features existed and or changed within PHP that we would see a lot more applications and source library packages created with better structure and foundation. Also, I believe that this would also assist in converting .NET developers to PHP or at least make it easier for them to support PHP applications within the workplace.