![]() |
figure 1. |
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. |
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. |
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. |
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 5. |
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. |
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.
Comments
Post a Comment