tags:

views:

48

answers:

2

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.

A: 

Ok, its taken me all of the last two nights and tonight (:-S) but I think I have an answer so I will post it to see if it helps anyone else.

I will start from the '//database connection' line

  • database connection is made
  • skin is chosen
  • 'buildFromTemplates' function chosen from the 'template' class.

Set the parameter to the page you are trying to build. The layout of the page you wish to create should be stored under skins>templates>filename.

The constructer called when executing the template class will then initiate a new Page instance.

This buildFromTemplates function then takes the parameter aka the file name and then retrieves the content from the file. This will be stored in the $content variable.

  • the database query is then made and executs the cacheQuery function
  • the addTag function for the Page object you have instantiated is then called
  • the title of the page is then set

Finally,

  • the parseOutput function is called which runs the replaceBits, replaceTags and parseTitle functions. This replaces the respective code written with the page you are trying to create.
  • Finally the content is output to the page.
James
+1  A: 

Firstly I think they over complicated the implementation of the Registry pattern. I always used the following approach which is more straightforward (I'll print a simplified version of it).

class Registry {

  protected static $_instances = array();

  public static function add($instance, $name) {
    self::$_instances[$name] = $instance;
  }

  public static function get($name) {
    return self::$_instances[$name];
  }
}

The Registry combined with the Singleton is 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 in skins/ not being part of the parameter send) overwrite the variable holding the name $bit with 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 the Page class.

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 queryCache for later use. For example:

// assume $db is a reference to the database object and that this is the first
// query
$db->cacheQuery('SELECT * FROM whatever');
$results = $db->resultFromCache(0); // previous query resource_id

.... 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.

mhitza
Thanks for your attempt at answering! I think I have just got my head around how THIS framework works, but I have absolutely no clue as to what makes good/bad framework. It was however the only tutorial I managed to follow so I give it some credit!I started with Zend but that was when I had no OOP knowledge, now I have a little I may be able to go back and try that again. I completely appreciate what your saying though. Maybe it was good practice learning the hard way? It is also better, I think, that how I was doing it before i.e. new files for every pages, messy directory etc.
James
@James, well for starters a framework that dares to call itself OOP doesn't use errors. Follows OOP concepts and does not patterns "just cause". There are so many OOP flavored PHP frameworks out there, choose the one most clear to you, use it for some projects, learn how it works and move onwards. Only after using a couple of them will you realize the benefits/compromise each of them has. And they you will see what is a good framework for your needs. Have fun.
mhitza
Sounds great, thanks a lot for the advice, it will definitely be followed. This is my first time using Stack Overflow but seems like a great site with very helpful people!
James