2

Taste of JavaScript in Php

Well, It’s true that Php was not matured enough in it’s early days but now Php become very so much powerful and recently Php 5.5.x has been arrived with some cool new features which are really powerful additions to Php so thanks to Php who are working hard to make this language powerful and robust. Anyways…

If you don’t want to read the full article and just want to find out what I’m actually talking about then you may directly jump here.

If anyone ask me to mention only one feature of Php 5.x that I love more then the answer would be definitely Lambada, support for anonymous functions, also known as closures, which is one the most exciting, powerful and most useful feature since Php 5.3, though in Php 5.3 there were some limitations in anonymous functions, for example, in Php 5.3 keyword $this was not allowed in an anonymous function, which means, in Php 5.3 if you are using a Closure inside of a class, the Closure will not have access to $this, You must send a reference to $this, but, you can’t send $this directly, for example :

In Php 5.4, the support for $this in Closures has been added. That’s a good news. Thanks to the developers of the language, once again. In Php 5.4, there are also two new functions has been added, which are really very helpful with Closures and these are bind and bindTo. These functions used with Closures to change or set the object binding, which means, someone can specify, which object a Closure should be bound to, for example :

Since, I’m familiar with these new features of Php, I was thinking that, if I could write Php code just like JavaScript, for example, in JavaScript we can write something like this

This is really cool! I love this language. This language is different than Php and it is a classless language, instead it’s a prototype based OOP language.

Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as classless, prototype-oriented or instance-based programming. Delegation is the language feature that supports prototype-based programming.

Read more about prototype based programming and example of JavaScript Prototype.

So, I was thinking that if I could write same code in Php and one day I just started to write some code and wrote something like this

So, I didn’t success, Php raised a fatal error and it’s logical. The stdClass is Php's generic empty class, one of Php’s predefined classes kind of like Object in JavaScript but not actually used as universal base class for an object. Which is just like an empty container, only can hold dynamic properties and used as type of object

Another Example :

So, $obj = new stdClass means that $obj is an object and an instance of stdClass and it can’t house dynamic methods like JavaScript but it can hold dynamic properties and it could be a scalar variable or even an array. For example, if you write

So, what I want to do is that, to add dynamic methods in an object, that is already an object instance made by Php using the new keyword and I thought that, I can use stdClass but it’s not possible using this generic class, so I need to use something else to simulate the prototype based programming style in Php (not 100% original but make it look like that), so now ?

Finally it’s done…

Now I can write code like JavaScript in Php just like this :

So, finally, I’ve figured out a way to create dynamic methods but without stdClass, instead using my own stdObject class that, I’ve written below :

So, now I can use it like JavaScript, just like this :

Alternatively, you can use a trait, available since Php 5.4 to wrap all the functionality of the stdClass, something like this

Now, create classes and use the trait like :

So, now you can use them as

Well, this is not same like JavaScript and I’ve just shared my random thoughts, an idea that came in to my mind and nothing else. If someone wants to give me any feedback then feel free and if someone can modify it to improve then please go on and share your idea. Thanks!

→ An updated version is available here and this one also requires PHP 5.4+ but much better, IMO.

Latest Blog