开发者

Help comprehending how the following framework fits together:

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜