News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Stoyan Stefanov's Blog:
JavaScript-style object literals in PHP
March 25, 2011 @ 09:02:59

Stoyan Stefanov has a new post to his blog looking at bringing something to PHP that Javascript developers have always had - object literals. These literals allow you to assign object properties at definition time instead of the usual PHP method of adding them either in the class definition or after the object exists.

Stoyan starts with an example of the object literals on Javascript and shows how it's (sort of) possible to replicate that wit type casting an array to an object in PHP. The tricky part comes in when you try to recreate the method handling JS gives in objects. Closures in PHP 5.3 comes close, but there's a few things he points out that just won't work the same way.

You can simulate some of this with __call method handling in a base "JSObject" class. For more details on his work bringing Javascript over to the world of PHP, check out his presentation from this year's ConFoo conference.

1 comment voice your opinion now!
javascript object literals tutorial closure call


blog comments powered by Disqus

Similar Posts

Vance Lucas: Handling Exceptions in Gearman Tasks (Even Background Ones)

Software Development Video & Tutorials: PHP and Couchbase

PHPFreaks.com: Working with checkboxes and a database

The Shadow Fox Network: Create Dynamic URLs With Mod_Rewrite and PHP Functions

PHPEasy.co.uk: Design Patterns - The Singleton Pattern


Community Events

Don't see your event here?
Let us know!


library extension version community opinion example series conference interview laravel podcast php7 performance symfony2 release voicesoftheelephpant framework install introduction api

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework