HomeArchive 2018Object Oriented WebApp Framework in PHP

software times™  Essays...

May 2, 2018

Object Oriented WebApp Framework in PHP


When after years of faithful service my procedural php framework became overly convoluted I decided it was time for a total remake OOP style. The crucial first step in OOP style programming is to conceive and design your objects. HTML5's semantic tags were my inspiration.

Notice that I didn't say "classes" but "objects." To think clearly I need concrete objects to work with, not abstractions.

Portfolio

What is a Website?

A website is an object made up of web page objects.

A web page is an object made up of HTML tag objects.

An HTML tag is an object made up of other HTML tag objects and an optional content object.

HTML tags have a hierarchy, the outermost tag, the "html" tag, can only have two tags, "head" and "body."

The head tag is made up of tags that give directions to the browser to load resources and tags to inform bots about the content of the page.

The body tag is made up of tags that display stuff on the screen. With HTML5 they added semantic tags to help people and machines better understand the intent of the tags. In principle the body tag should have one of each, "header," "navigation," "content," and "footer" tags.

The "content" tag contains the business of the page. The "navigation" tag has the menus to navigate the website. The "header" and the "footer" should be self explanatory.
The two principal aims of architecture in construction are to create buildings that are both pleasing and functional. Software engineers spend a lot of time and energy to come up with the right "architecture" for their projects but unlike in construction the architecture is less about the user experience and more about the quality of the code which is more engineering than architecture.

Having been a procedural coder most of my professional life I'm convinced that OOP is the better engineering alternative for websites. HTML5's semantic tags were the inspiration for my OOP web app framework. There are two parent classes, Tag and Page, and any number of helper classes such as Database, Date, etc. Here I'll only cover the Tag and Page classes which are the pillars of the framework.

Class Tag {}

class Tag {}
class TagHTML {}
class TagHead {}
class TagBody {}
class TagHeader {}
class TagNav {}
class TagContent {}
class TagFooter {}
The "Tag" family has seven children. With the exception of TagContent these child classes are mostly static html. TagHead can receive requests from the active Page object for additional tags requesting additional CSS and JS resources. The content of the TagContent class is created entirely by the active Page class. The Tag family will render the well know HTML structure
<!DOCTYPE html>
<html>
<head>
  Head tags
</head>
<body>
<header>
  Header stuff
</header>
<nav>
  Navigation menus
</nav>
<content>
  Content generated by the active Page object
</content>
<footer>
  Footer stuff
</footer>
</body>
</html>

Class Page {}

class Page {}
class PageIndex {} // index.php
class PageAbout {} // about.php
class PageAboutAuthor {} // about-author.php
class PageContact {} // contact.php
The Page family has as many child classes as the website has pages. The child class name is derived from the page file name as illustrated above.

If the page contents are static the child Page can handle it all by itself. If the page's business is simple the child Page can handle it. If the business is more complex and if it is shared by several web pages then it's best that the business should have it's own class or family of classes and then it is injected into the child Page class to keep the separation between business and view. This will be covered in more detail in a future post.

OOP controller

The page files invoke the OOP controller with two lines of code, that's all they do:
$include = "/home/user/includes/";
include "{$include}oop.php";
The controller invokes autoload, builds the child Page class name, creates an instance of the child Page class which builds and returns the page contents, creates an instance of the TagHTML class passing it the page contents and finally renders the page in nine lies of code!
require_once "classes/_autoload.php";

$filename = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
$filename = str_replace('-', ' ', $filename);
$filename = ucwords($filename);
$filename = str_replace(' ', '', $filename);
$pagename = "Page{$filename}";

$content = new $pagename($include); // create page content

$page = new TagHTML($content); // create webpage
echo $page->getTag(); // render webpage

Conclusion

This web app framework (I still don't have a name for it) is a lot simpler than the general purpose frameworks I have looked at. It has a limited scope, to create websites and web apps. It uses inheritance sparingly, only in tight knit families of classes.

So far I have used it for only one web app, my Portfolio manager. It has been easy to add functionality and new services to the Portfolio. The web app has approximately 40 pages, it uses (my simplified version of) AJAX. The framework does not require an IDE, I manage very nicely with BBEdit.

Denny Schlesinger

Share this article with your followers

Top
HomeArchive 2018Object Oriented WebApp Framework in PHP
Copyright © Software Times, 2000 - 2015. All rights reserved.
Last updated May 2, 2018.