Jump to content

Implementing a slim page class


maexus

Recommended Posts

I will start with saying I'm well versed in PHP but never saw a need to implement OOP in any of my projects. I have found it serves a need of mine to keep code clean and neat. Using a page class, I can call the page headers with the construct and call the footers when the script stops running. Keeping along this minimalist mindset and a desire to keep the code as compliant and readable as possible, it made sense to make the doctype, title, css files, ect... properties of the page class. This is basically what my script would look like so far:

 

$page = new page();
$page->doc_type = "HTML 4.01 Strict";
$page->title = "Front Page";
$page->css = array("stylesheet"=>"main.css","alternative"=>"alt.css");
$page->requires_login = 0;
$page->min_user_lvl = 1;

 

This obviously doesn't work because you are defining variables after the construct was called.

 

Now, I could declare those variables in the script and pass them as arguments when calling the page class but I would really like to avoid this. Like I said, I have a focus on keeping code compliant and using features as they were designed to be used while keeping clean and minimal code. Does anyone have any advice?

Link to comment
Share on other sites

Just have another public function called "render()" and run that last. It should build the page from the components supplied.

 

The problem is you can't expect the page to "know" what you're about to specify after you're created an instance of it.

 

Alternatively try using a pageHelper

i.e.

<?php
class pageHelper {
  public $doc_type;
  public $title;
  public $css;
  public $requires_login;
  public $min_user_lvl;
}

$pageHelper = new pageHelper();
$pageHelper ->doc_type = "HTML 4.01 Strict";
$pageHelper ->title = "Front Page";
$pageHelper ->css = array("stylesheet"=>"main.css","alternative"=>"alt.css");
$pageHelper ->requires_login = 0;
$pageHelper ->min_user_lvl = 1;

// Hand the helper to the page.
$page = new Page($pageHelper);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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