Skip to content

Instantly share code, notes, and snippets.

@chalasr
Forked from InFog/proceduralphp.md
Created February 10, 2016 00:30
Show Gist options
  • Select an option

  • Save chalasr/b0ed5c168a35ef2e5a7a to your computer and use it in GitHub Desktop.

Select an option

Save chalasr/b0ed5c168a35ef2e5a7a to your computer and use it in GitHub Desktop.
PHP Procedural Framework Manifesto

Procedural PHP Manifesto

We are web site developers (a.k.a. webmasters) and we just want to get stuff done. We don't need Object Orientation and we don't need Design Patters and other boring and not easy to learn and understand stuff to get in our way to get our sites up and running.

And that's why our values are:

  1. Procedural Code over Object Orientation
  • Classes with only static functions are ok, since it's just a way to keep functions in one place. And is still procedural
  1. Explicitly load what you need over autoloaders
  • Autoloaders are complex and can fail. Just include() what you need
  1. One PHP file per URL over Front Controller
  • Just include() header.inc.php and footer.inc.php in your files and everything will be ok
  • And also, Front Controller reminds us of Controllers and MVC and this stuff is complicated
  1. Inline MySQL queries over Database Abstraction libraries
  • These libraries are usually OO, and we don't like OO

And remember that we don't only add more value to the items on the left, we simply ignore the values on the right.

PHP Procedural Framework

To make our dreams come true, we developed our own PHP Procedural Framework. This is the simplest PHP Framework around because we have no time to learn how to use the huge OO-DependencyInjection monsters that surround us.

The framework is contained within just one file that you can include() in all your scripts (value #3):

<?php
include('phpprocedural.inc.php');

And that's all, super simple! Now you can use the life saving functions:

<?php
connect_to_mysql();
// In the first lines of 'phpprocedural.inc.php' you will find the config vars for connection

include_script('header.php');
// This function checks if the file exists, if not it shows an error

show_webmaster('John Doe', '[email protected]');
// Use this in the footer of the site. Displays something like this: Webmaster John Doe - ©Year - Visit Counter: 000004356
// How can we count the visits? Don't worry, we just can ;)

gracefully_close_mysql_connection();
// Will close MySQL connections. We don't want open connections to me left there, right?

You can open 'phpprocedural.inc.php' to read our code, but you don't need to. Our code is simple and awesome. Here is just a small part, if you want to know about our best practices:

<?php
function gracefully_close_mysql_connection() {
    // My cousin told me that @ will avoid errors when there is no MySQL connections. Great!
    @ mysql_close();
}

Bonus! No more errors, now you can become a PHP Pro in no time!

We all know that errors in the code, like undefined array keys are boring and only confusing for our clients and users. In the last version of our framework we added some magical lines that will make you a PHP Pro with no more errors display!

Here is how the magic is done:

<?php
// Magically removes errors from the code
@ error_reporting(0);
@ ini_set('error_reporting', 0);
@ ini_set('display_errors', false);

Join us! Spread the word! Use the tag #proceduralphp :)

PS: This is just a joke -.-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment