In demand

ad1

Wednesday, August 12, 2015

Non-programmers Guide to How Frameworks Work

figure 1.
How does a content management system like Drupal or Wordpress work? Why are PHP frameworks like Laravel so popular?

After reading this you will understand  the inner workings of most PHP Frameworks, and  content management systems like Wordpress or Drupal. You'll do this without having to be a programmer or know any programming. As a matter of fact you'll be able to explain to actual programmers how they work in the real world.


As an added bonus you'll learn and understand many of the tenants of web programming like :
  • Design patterns without having read any long computer science text. 
  • Know the difference between complex and complicated.

The teaching tool I'll be using is a childs game called "Simon Says".  This is a very important piece of learning because "Simon Says:" is an interactive game which requires a  virtual user interface, commands and error handling. And just like a computer program or web application its dependant on the idea of control flow.

Request and Response

When a web browser requests a web page by navigating to a URL like http://blog.fhqk.com  a list of things start happening in the background. What these things are might be different depending on the software used at the URL. What the more popular content management frameworks like Wordpress and Drupal have in common is the way that they build a response to requests. Understanding the building blocks of common responses is the single most important piece of knowledge that a web developer or designer can have.

Control flow

Once a request is  recieved a  web page  goes about the task of  setting the order in which the individual statements, instructions or function calls of an web application are executed or evaluated for a response. This is called control flow or flow of control.

Both request/response and control  flow are easily seen and demonstrated in the childs game called Simon Says:

A command starting with "Simon says:" means the players must obey that command. A command without the beginning "Simon says:" means do not do this action. There can be very complex and difficult command chains, such as "Simon says: Arms up". "Simon says: Arms down". "Arms up." Anyone ending with their arms up is eliminated, because you cannot obey a command that doesn't begin with "Simon says:".


figure 2.
Within  the rules of the game you can do some complex operations. You can also break down long list of operations into simple tasks.We are going to use the game to describe how a web application works. A simple flow is no problem.  A request is sent and a response is made. A few simon says commands later and you get a browser page for a response. The responses though are a very static ones because of  the  simplicity and number of the events used to produce them.  To get a more dynamic and comprehensive response  several more detailed events are needed.

 Event loop

 To build multiple events for a single request and to deliver multiple events into a single response. Drupal and Wordpress use a more advanced control flow something  called an event-queue or event loop. The functions called in the control flow loop are called hooks.

figure 3.
To give you a better understanding of how this works let's make the simon says game more advanced. By using it to teach a popular viral dance, the Macarena. Which is a closer approximation of  Drupal and Wordpress with all their eclectic styling. We' replace the simple simon says instructions with  music and lyrics for the request being sent. We place all of the required dance moves that are needed and some extra gyrations into one long sequence or queue. To get everyone to dance Macarena the game leader has to cue the music then go through  the dance sequence. When a required move in the sequence applies to dancing the Macarena the leader says "move!". The game leader loops through the dance moves again and again until all in the audience are in the game.

Observation

That was easy. So why is it that most PHP frameworks are so complicated? It's because many are using something called an observation pattern. There are no event-queues with hooks and the control flow is only implied. Many trained programmers consider this system to be more flexible, easier to understand, scalable and easy to maintain in large applications where many things must happen simultaneously or out of order. Somethings may not start until a particular request is made and the response may come out of sequence. Event-loops don't fit this environment well. This is the main reason why projects like Drupal are switching to using frameworks like Symfony2 that make use of observation.

figure 4.
To better understand this lets scale up from a room full of Macarena dancers at a club to a stadium with thousands. You can't just throw out some the Simon says game leaders into the crowd and expect good results. But put a bunch of performers on a central stage that the crowd can observe and listen to then getting the desired effect is much better. The public is expected to listen to the music and just do what the performers do at the right moments. though some others in the crowd might not do anything.



This works fine and well in a stadium where the entire audience is facing the center of the playing field. Go a bit larger  and more spread out without using  a central stage  to co-ordinate things and  unexpected complications arise. Finding out what's wrong is impossible.

Out of control 

But PHP Frameworks are not content management  systems and generally do not have a request and response connection to control flow via observation. Perhaps some parts of them are ready to go while others are sitting and waiting to respond to a request. This  is called decoupling  from the events which is the cause of complication in a large system.  So it is necessary to provide a connecting code and organization of an application. Code that builds groups called listeners and subscribers. Some code listens for certain cues and other code looks in or polls to see if it has something to do. But since there is no control flow lots will happen all the time with every request in different places.  So the framework needs something to replace the event-queue  in  web application or content management system to co-ordinate and deliver the responses of all the groups.

figure 3
figure 5.
Now to explain this need for control using Simon says to teach the Macarena with the  required connection and some changes. Listener groups will only react to the musics requests. Subscribers will only react to what they can see because they are observers.  Without the control of a  very simple  game with  properties for easily handling complexities things become very complicated and the outcome is not optimal.


More control

MVC has become the most  popular ways of adding more control to the observer pattern for a reason. It's easy to implement  and even easier to modify to get different responses at run-time.  What's run-time? Look at the image above ... uh yeah. That's! run-time. With MVC you can build multiple and different event-queues in the background to hold control of system events.

Without additional connections  to the audience everything would have gotten out-of-hand very quickly especially if there are several groups that do different  parts of the dance and have to synch up. So instead of  sequence of moves that are the Macarena everyone starts to dance in common patterns found in all popular dances. Then they change  and  adapt commands to eliminate the parts they don't understand. Only a few participants, the one closest to the first event look like they are doing the Macarena. To get more control over what they see and do you will have to add a real life version of a pattern like MVC.


figure 6.
Think of MVC as a dance chart way of learning the Macarena without playing the Simon says: game or observing or listening to the music. The complexities of the dance are reduced to a simple set of  visual rules to follow. Listen to the  music sequences and obey the chart and the right dance is taught one step at a time. Think of it as colored footprints for the steps that "Simon says: " to follow. Doesn't matter what extra style gyrations are added in by the crowd. It's cool because it separates the things you are most concerned with into  three items you can process and practice  the dance steps, the music and timing, individual stylings like hip shaking are optional. DJ cue the beat!


video
Now it's easy to see that without proper training and guidance you can become immersed in the complexities of a system. Having so much complex activity happen in different places without explanation makes everything seem complicated. Many times in software this is the reality and it takes lots of  time in reading and practice to come to a place where you understand the complexities and can use them.

Using  content management system hides the complexities and makes it all simpler which is why they are so popular.  Frameworks also hide the complex internals of the observer pattern. Both make having a web application a breeze but also make it difficult to learn PHP or web development just by looking at them.

The video for the songs is a perfect example of  this happening. It hides all the complex production work  like the music, changes of scenes, camera angles and different participants. It is very enjoyable though it makes it difficult to learn and do the Macarena. But  by using  the dance chart above and listening to the flow of the music. It becomes simple.

 

 Summary:

If you’ve ever used a web browser , then you’re well acquainted with events. Every time the user interacts with the program — clicks a button, pulls down a menu, or presses a key — the content management system within it generates an event-queue using hooks. In a framework it throws event objects , and your job is to grab them and build responses and behaviors.



Today's Links

Post a Comment

Print this!