Extending PHP 5.3 Closures with Serialization and Reflection

PHP 5.3 has brought with it some powerful and much-needed features like late static bindings, namespaces, and closures (also referred to as anonymous functions and lambda functions). Anyone who is experienced with JavaScript or who has worked with programming languages like Scheme or Lisp should realize the value that anonymous functions can bring to PHP. The PHP Manual explains closures like this:

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses. Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class.

PHP has very few predefined classes that are part of the core language, so naturally I was intrigued by the Closure class. The PHP Manual has this to say about the class:

The predefined final class Closure was introduced in PHP 5.3.0. It is used for internal implementation of anonymous functions. The class has a constructor forbidding the manual creation of the object (issues E_RECOVERABLE_ERROR) and the __invoke() method with the calling magic.

The invoke magic method is also a new feature in PHP 5.3. It is called when an object is used in the context of a function (e.g. $object($parameter);). Since Closure objects will be used like functions, this is a critical feature of the Closure object. The Closure class may be perfectly equipped to act like an anonymous function, but it does not provide any extra utility beyond that. A var_dump() of a closure will reveal the functions parameters, but there is no way to get any other information about the Closure (like the actual code of the function). Trying to serialize the Closure throws an Exception and json_encode() just returns an empty JSON string. To make matters worse, the Closure class is final, so there is no way to extend it.

That simply wasn’t going to cut it for me. I wanted to make my own Closure class that was at least able to do the following: Read More »

Posted in: Articles, Development

Senior PHP Developer Position in Tempe, Arizona

synapse_office

Our company Synapse Studios is hiring a senior PHP developer right now. Specifically, we’re looking for someone who can help lead our talented team and establish methodologies that scale and make for a more powerful team. More information on the position is available here, but you’d be working in our offices in downtown Tempe, Arizona, right on Mill Avenue. It’s a great space with fun people and exciting work, so if you’re interested, view the job post on our site and follow our instructions over there.

Senior PHP Developer Job Posting | Synapse Studios

Posted in: Announcements

Is PHP 5.3 Terminal? Well, It’s Getting One For Namespaces

A language lives and dies by how easy it is for a person to express something within that language. But the ease in expression is much like the type I and type II error rate in a statistical test: as you adjust one to be nearer to where you want it to be, the other gets farther away. So the architects of a language have to choose between adding more keywords and constructs or having longer, but simpler sequences of existing ones.

PHP is a very easy language to learn and use because it has a syntax that is a simplified sampling from C++/Java and Perl. People who use these languages pick up PHP fast, and people who haven’t used any languages often learn PHP with as much ease as they would learn Perl.

Read More »

Posted in: Articles, Development

You Suck At Programming And I Hate You: Things NEVER To Do In PHP & SQL

One of the more exciting (by which I mean soul-crushing, murderous-rage inducing) things about my job is getting to look at the terrible, terrible code that runs all sorts of different websites on the internet. Chances are, you wrote some of this terrible code; I know I did.  It’s even possible that you still write such code. In case you do, go grab yourself a stiff drink (a strong vodka martini, or gin and tonic is recommended) and get comfortable, because I’m about to lay some edumacation on you.

Things You Should Never Do

First, I’m going to talk about some things I’ve come across that you should never, ever do. If you do these things and I ever have to work on your code, be prepared for the fury of ten-thousand burning suns to come crashing down around you, for I have warned you. Pay it forward by writing decent code, for one day you may find yourself having to maintain someone else’s heap of terrible code and woe, for you shall feel some tinge of guilt, having made other developers go through your new hell.

All of the snippets in this article were found in actual code being used in the wild. (And most of them come from a single, disastrous, amalgamation-of-fail file.)

Read More »

Posted in: Development, Rants

The Lazy, Clever Programmer: A Compendium Of Code Reuse & Recycling

I started getting “serious” about development because I had a desire never to write lengthy, wandering streams of code again.  It was not for any reason but unadulterated laziness—the kind that so overpowers the better senses as to force a person to spend hours in a chair with the express goal of not spending hours in said chair.  It’s a wild, consuming laziness that seems to know no bounds.

As developers, once we start separating our code into abstract ontological typologies, we make use of the human mind’s phenomenal ability to work with types.  Our code becomes less about jump tables and registers and more about users, email messages and images.  What once was a problem of allocating resources and operations within the computer becomes an abstract, logical problem within a collection of objects.  Like children awe-struck by stories of magicians of old, speaking incantations and pressing their wishes into reality by the power of their mind alone, we become drunk with the sense of awe and possibility.

We *really* dive into things after the jump, so go ahead and hit it.

Read More »

Posted in: Articles, Development

Book Review: php|architect’s Guide to Enterprise PHP Development

Our office purchased a copy of php|architect’s Guide to Enterprise PHP Development (TOC) by Ivo Jansch last week, and I called dibs on the review. In it, Jansch sets out to identify tools and methodologies PHP developers can use (and have traditionally not used) to increase their chances of success. Jansch points out that PHP rapidly went from a tool used mainly to develop Personal Home Pages (I don’t know why I capitalized that… so weird) to an increasingly well-regarded enterprise-level platform. Unfortunately, while the open source community surrounding PHP is one of the most active and vibrant ones around, it has been reluctant to pick up some of the more formal development processes that the .NET and J2EE platforms are known for. We’ll dive deeper into the book in about 20 pixels. (You might have to click on a link or something, just a heads-up.)

Read More »

Posted in: Reviews

Enterprise Patterns: A Look At Application Service

Today we’re going to talk about PHP enterprise patterns and all the fun they bring to the party. We’ll get into front controllers and take a close look at MVC as well.

Towns, Buildings, Construction

So let’s talk about patterns. Specifically, I’d like to talk about the MVC architecture pattern, and some thoughts I’ve had recently regarding its application. Let’s first clear the air a bit, make sure everyone is limber, and define our terms. It may be unknown to some that the seminal work in patterns, A Pattern Language: Towns, Buildings, Construction, was actually about architecture (the kind with buildings) and urban planning, not software design. One of the key observations the author makes in the book is that many cities which are laid out in a practical and attractive manner follow a similar pattern, which was varied depending on the exact situation. This is exactly how patterns in software architecture work; Patterns are elegant solutions to common problems that are flexible enough to be applied to differing scenarios.

Read More »

Posted in: Articles, Development

Function References and Runtime Functions in PHP 5

Well, it’s the final hour of the final day, so it’s time for my weekly mandatory blog post. I was going to counter-rant a hack-job post and call the author a chump as much as my fingers could muster. But I was told that “Chump!” three hundred times is not a blog post. As I was thinking of other topics to write about I realized two things. First, no one cares that much about Lindsay Lohan anymore. Second, I have a hard time deciding what to do.

But it is this distinct lack of commitment that, while being so destructive to my personal relationships, has brought me into a rarely discussed nether-region of PHP functionally: function references and lambda functions. The sad thing is there isn’t really either in the language – at least not what you’re thinking of if you just read “function references” and “lambda functions” and though, “Oh yeah! Those are awesome!”. No, I’m going to talk about the dark and shameful ways in which we make do without these features in PHP. That feeling you just got? It’s called excitement. Actually, it’s probably indigestion, but get it checked out anyway. All good? Follow us after the jump for a look into the depths of PHP 5.

Read More »

Posted in: Development

Meet Jeremy Lindblom: Intern

My name is Jeremy Lindblom, and I am the new summer intern here at Synapse Studios. Synapse Studios contacted me after reading my resume on the ASU Sun Devil CareerLink website. After an extensive interview and a very extensive waiting period, they brought me aboard. I’m excited to jump in to some enterprise-level development and really start to learn the exciting, and constantly-changing, field of web development.

This fall, I will be starting my senior year at Arizona State University. I’m studying Computer Science with a concentration in software engineering. My passions are definitely in the web development arena. I aspire to become a skilled developer, and to produce quality work that I get payed well for. I have also been accepted to the Integrated Masters Program, and will simultaneously start grad school throughout my senior year.

In the past I have developed mainly with XHTML, CSS, and PHP, and have done many projects over the past few years. Recently, I have been working with CampusSpots.com to develop their website and prepare it for expanding across the country. Other personal projects include Finding Restaurants and Regular Heroes (my band).

Though web development is fun, I often find my attentions divided to my other interests. I love performing. I love to participate in musical theater, ballroom dance, choirs, and other performing. Right now I play keyboard and sing backup for Regular Heroes. I also enjoy writing piano and choir music, lyrics, poetry, and fictional stories. Other interests include making funny faces, picking up loose change, religion, and dark chocolate.

I live in Mesa, AZ, with my lovely wife Penny, and we don’t have any kids yet.

Posted in: People

Meet David Bernal: Developer

My name is David Bernal and I’m one of the developers at Synapse Studios. I’ve been with the company for nearly 2 years, which, at this company is fairly long. I’ve been chest-deep in code since day one and have worked for various clients and on many personal projects before then.

As a developer, my primary contributions to this blog will be about enterprise patterns and reuse ability as we implement them in client projects and particularly in the Canopy Framework. One of my particular strengths is the wide variety of experience I have beyond the realm of PHP-based web development. In a previous life as a desktop application developer I worked primarily with C# and so I have experience with the things the .NET library does well and those which PHP does better. Based on this experience, I’ll be working with the team this summer to incorporate some exciting automation and reusability enhancements into the framework. Stay tuned, as the coming months promise to be exciting times for the web’s next best application platform. [That's Canopy, we think]

Posted in: People