I am just getting into OOP and framework design. I have started by using the following three tutorials;
http://net.tutsplus.com/tutorials/php/creating-a-php5-framework-part-1/
http://net.tutsplus.com/tutorials/php/create-a-php5-framework-part-2/
http://net.tutsplus.com/tutorials/php/create-a-php5-framework-part-3/
This is the first framework I have tried to work with and because the tutorial is not aimed at complete novices I am finding myself having to reverse-engineer the code to see how everything works. The problem is I’m stuck.
First, I understand the basic concepts of the framework including the directory structure.
Second, I understand what the registry and database class are for (although I don’t fully understand every function within them just yet).
My problem comes with the index.php, template.class.php and page.class.php files. I broadly know what each should do (although further explanation would be nice!) but I do not get how they fit together i.e. how does the index page interact with the template and page objects to actually produce the page that is displayed. I especially cannot work out in what order everything is called.
The index appears to me as:
- require registry class
- create instance of registry (don’t quite get this bit – easier way to access database?)
- store the database and template objects
- creates new connection from the stored database object
- choose the skin for the page
Then, and here is where I get lost:
- build actual page via buildFromTemplates function (which I can’t get my head round)
- cache a database query
- assign tab (i’m completely lost as to what a tag is!)
- set page title
- display content
Can anyone help break this down for me? I tried Zend before this but it was far too complicated, this one is more accessible but as you can still has me stumped (although I have started to understand objects FAR more by trying).
Thanks in advance.
Firstly I think they over complicated the implementation of the
Registrypattern. I always used the following approach which is more straightforward (I’ll print a simplified version of it).The
Registrycombined with theSingletonis just a mess.Regarding the aspects where you got lost:
1. buildFromTemplates
Method accepts unlimited numbers of parameters
func_get_args()as template file locations, either relative or absolute. If relative (as inskins/not being part of the parameter send) overwrite the variable holding the name$bitwith the absolute location. If the file exists read in the variable$content. Repeat until all method arguments are used and add the end result to thePageclass.2. Query cache
If the given query does not return a resource_id (which a database query should) it means the query didn’t execute successfully and the method triggers and error. Otherwise save the resource_id in the property
queryCachefor later use. For example:…. ahhh, forget it.
This is so messed up, rather recommend you start with some sane framework good for learning internal works. Like CodeIgniter, and move onwards when those concepts are clear.
This tutorial is full of wrong decisions, like errors instead of exceptions, tight coupling, custom template engine (where plain PHP would have sufficed) and more.
Even symfony, which is a big framework is not that hard to follow along the lines.