Jump to content

A few small questions


Razesdark

Recommended Posts

Right, first time I'm posting a topic here so bare with me :)

 

Today I was looking at some tutorials regarding making a Template based website.

I haven't figured out yet wether I was going to make my new website template based or not so I thought I'd stroll around to see how others made theirs.

 

Mostly of my PHP knowledge is about 4 years old and have been resting for quite some time untill recently. I the past I did fool around a bit with classes but not so much as I didn't quite see it's advantage over functions.

 

Anways, when I did found a tutorial I found a bit of coding which I didn't quite understood. So I thought, let's ask the lads/ladies at phpfreaks! I did try to find it at the php manual but wasn't quite sure what to look for.

 

The first question is this.

The tutorial made a class named 'Page'. Inside it was a variable $page.

Then came this sentance:

$this->page = join("", file($template));

 

Now I did know $this->variable from when I toyed around a bit with classes but I'm not familiar with this kind of notation.

What exactly does it do ? I understand the join part.

 

Now the second question is regarding this part:

$page = new Page("template.html");

What is the purpose of the 'new' ?

The Page prolly refers to the Page function inside the Page class. (yes all confusing)

 

Here is the entire code to make a bit more sense to the story.

 

<?php
class Page
{
  var $page;

  function Page($template = "template.html") {
    if (file_exists($template))
      $this->page = join("", file($template));
    else
      die("Template file $template not found.");
  }

  function parse($file) {
    ob_start();
    include($file);
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
  }

  function replace_tags($tags = array()) {
    if (sizeof($tags) > 0)
      foreach ($tags as $tag => $data) {
        $data = (file_exists($data)) ? $this->parse($data) : $data;
        $this->page = eregi_replace("{" . $tag . "}", $data,
                      $this->page);
        }
    else
      die("No tags designated for replacement.");
  }

  function output() {
    echo $this->page;
  }
}
?>

<?php
require_once("lib/template.php");

$page = new Page("template.html");

$page->replace_tags(array(
  "title" => "HOME",
  "descript" => "Welcome to my website!",
  "main" => "dat/index.dat",
  "menu" => "dat/menu.dat",
  "left" => "dat/submenu.dat",
  "right" => "dat/right.dat",
  "footer" => "dat/footer.php"
));

$page->output();
?>

 

Source:

http://www.codewalkers.com/c/a/Display-Tutorials/Writing-a-Template-System-in-PHP/7/

 

Link to comment
https://forums.phpfreaks.com/topic/138347-a-few-small-questions/
Share on other sites

1) Because 'file()' returns an array of each line, 'join()' groups those lines together so basically it's just grabbing the files contents and sticking in the '$this->page' variable.

 

2) 'New' is used for classes. All it does is create a new page object and as it's made for PHP 4+, it has a function named 'page', this forces you to enter a page name. So basically, you create a new page object, assign it to the '$page' variable and then as the class contains a function name the same as the class name, it forces you to enter a value.

 

You wouldn't use 'new' for functions.

 

Hope this has helped :).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.