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.

I can see the potential value of 4, although I have to admit that I’ve yet to come across a specific need for it in my development.
Re: 5, it really is trivial to implement get/set functionality using __call and inflection so that get/setPropertyName() do exactly the same as your C# example. I can see the advantage of having this “baked in”, but agree with some of the other comments that there is a fine line between functionality and the cleanliness/simplicity that has made PHP so popular.
Can I add another item to the list?
Some means of so-called horizontal re-use like traits or grafts would be great – I’ve just blogged on a way of implementing trait-like functionality in PHP:
http://www.stevehollis.com/2010/04/trait-like-functionality-for-php/
Thanks for the comment Steve and Luiz!
In addition: it would be great to have alternative syntax for declaring arrays, [] instead of typing array(). These brackets are not used in PHP for syntax.
And about the post: the 4th issue really matters for me, I can leave without others…
@Otar
I completely agree. I am not sure why PHP uses array() for declaring arrays as many of other languages use a common syntax. However, I highly doubt that this will ever change.
Regarding the final keyword for class member variables, I think it should be implemented as it would be a great asset to myself and probably many of others who work with object oriented applications.
The double colon operator (::) was the first choice for namespaces, but there is a situation where the interpreter could not tell the difference between two options:
namespace Foo;
class Bar
{
static public function test()
{
}
}
And
namespace Foo::Bar;
function test()
{
}
They’re both called using
Foo::Bar::test();
So the backslash operator was the best choice left (three colon was the other choice, but IMHO that would have been stupid). Anyway, it’s not a big deal, you just have to get used to it. PHP already did weird choices about conventions, so it’s just one more.
I agree with the usage of the final keyword on attributes and nested namespace by the way. Namespaces should be always declared using brackets (like classes and functions) rather than declare the name as the first thing in the file, and everything else belongs to that namespace…
How good that PHP does not have features which are suggested here. Otherwise it would be as convoluted and weird as the C#.
C# mature? Is it a joke?
@Otar
Exactly. It would be great to have more flexibility in areas like array syntax. PHP is so verbose and so punctuation-heavy compared to other languages.
@Aaron
I had to laugh at your comment about not being able to read Chinese source. I’m sure Chinese people don’t appreciate having to read English source either. Why should English be the only language allowed for programming?
Also, this is a pretty random list; I can’t say I’ve ever wished for operator overloading. I don’t think method/function overloading really saves any time either… you can already create separate functions or use variable parameters, so the only benefit is when you happen to have all different attribute types such that you don’t need named parameters. How likely is it that you will have a class with exactly one string and exactly one int? Besides, we don’t even have the type hinting you would need to implement this, so maybe you should be asking for full type hinting first
It was refreshing to see a completely different list of PHP complaints though, so kudos!
Namespaces:
The namepace separator works great, I’ve done plenty of projects with it, and it’s a lot nicer than :: everywhere. (Not to mention that :: just is infeasible for PHP, as A::B::C() can’t be resolved [ Namespace A, Class B, Static Method C; or Namespace A::B, Function C() )
Also, you don't need to do things explictly with namespaces.
Strings should always be single quoted so that $targetClass = 'foobartheSorter'; $sorter = new $targetClass(); works properly, and that isn't an issue.
And then you can always go:
namepsace foo;
class bar { ... }
class interfacebar {...}
const interfaceSORT_KEY = 'reading'
class otherFoofooConverter extends otherFooConverter {...}
Encoding:
>> Problem is that if you take a language such as one of the Chinese based languages, developers who speak English will not be able to read their source. I know I have a problem with this, does anyone else?
No, for they have the same issues not being able to read English sources. Support for writing PHP code in UTF8 has been supported since 5.0; [6.0 was adding support for legacy encodings, like sjis]. I have worked on, and used many, many projects that were written solely in Japanese.
so the code looked like:
class ?? {
protected function ?? ($?, $??) {…
}
There are thousands of libraries out there that have not a single latin name, PHP 5 has always supported it (and possibly even PHP 4, I haven’t tested though)
Operators:
Why not, though I’ve never seem then done intuatively. I personally do the java hack-around by going forward with
if (foo.add(foo).equals(bar)
Having started with C and C++, written PHP for a good (well, up for question) decade, and then gotten into Python, I can absolutely identify with some of this.
I have often pined for operator overloading. While extending the ArrayObject class or implementing the ArrayAccess interface can manage some of the array operation ‘overloading’ that I’ve wanted, straight operator overloading is something I’ve missed ever since hitting the web development world.
It brings up a very common issue: People who use — and are used to using — PHP simply don’t have a mind for what other people want to do, or why they want to do it. If they can’t see the use for it or don’t understand it, they’re not interested. The larger problem is that the core PHP developers are exactly the same way, and when they do finally decide to do something we really want, they do it in a ridiculous, unorthodox manner (see: namespace definition).
Inner classes and multiple inheritance are two things I’d have placed higher than ‘final’ and C#-style getters and setters, but for the most part I’m completely in agreement. I’ve been using func_get_args far too much lately, and I dislike myself more and more every time I use such a horrific hack.
@Eric
Now that I think about it, I would definitely like to see at least inner class declarations and also member variable constructions/instantiations done inline similar to how Java capability.
Example for inline member variable declarations:
class MyClass { private $variable = new class Inline() { public function __construct() { } }; }Also,
@Eric:
I completely agree with what you stated regarding the common issue. And this is most definitely not only within the PHP community it happens constantly throughout everyday life.
I wish that php supported a short form for named parameters like ruby does, so that I could stop worrying about which comes first, the needle or the haystack
$ a = func(:a => ‘a’, :b => ‘b’);
@Eric:
Ruby supports operator overloading, and thus, Rails does as well (and so do all ruby web frameworks for that matter).
Was an interesting article, thank you..