Facet Style PHP Framework

Frameworks are all over these days, each one does something a little bit different than the others, trying to stand out. Honestly MVC does a really good job on the web. Building a website,or application? No problem, there’s an MVC framework for that. Whether it is Zend, CodeIgniter, Symfony or any other, they are used everywhere.

But what about where they aren’t appropriate? What tools do we have for when we want to add a piece of functionality to a site that either isn’t part of a framework, or a tool in the framework doesn’t exist? Well, you are left two options, build it yourself, or find some script someone wrote and do your best to incorporate it into your site.

Now this really isn’t so bad, until you have 3, 4, or 5 of these different classes, and scripts, and then you want them to integrate with each other. That is where the kicker really comes in. Then you have to spend time digging into these unfamiliar classes to patch together a relationship that just wasn’t meant to be.

So is there a better way? Maybe, I’ve lately been pondering the idea of a facet or module driven framework. This kind of framework may still embody some of the MVC patterns (“if it isn’t broken don’t fix it”), but lends itself to being more flexible.

For example, imagine a way to decouple modules of a traditional MVC like Zend or Symfony, and be able to use them on any site with ease. But, when multiple facets/modules are brought together, they know how to talk to each other and integrate.

The first question that arises of course is, “Is it possible to do with a small enough footprint in an easy way to implement and configure?”.

I don’t know.

There is also the question of performance, if you were to place all of the components together into its full framework, how would it stack up? Likely, it would be slower, but then, the idea isn’t to use it as a full blown framework. The idea is to be able to cherry pick framework-like pieces and use them together seamlessly, without being constrained.

Over the next months this is an area I will likely explore. To anyone who happens upon this article, if such a framework/tool exists, I would be more than happy to see it.

Traits and Multiple Inheritance

Up until PHP 5.4, there was no multiple inheritance; every line of OO code had to be written with single inheritance in mind. So when PHP 5.4 was released two years ago, many developers asked, “Why would I need Traits?”. This is a good question to ask, we have gotten by without multiple inheritance all this time, we rarely say “Man I wish I had multiple inheritance right now”. However, Traits open up a new door, a new potential path for organization and design. Now that it is possible the question will come up more often. In this post I am going to address this with a perhaps trivial example, but one that does work. This assumes the reader understands Traits and Namespaces in their basic form.

The code has been truncated in favor of a shorter article. Nothing import is missing, just getters and setters really. The full set is available here: TraitsAndMultipleInheritance-Code

 

Database

Let us say that we have a basic database setup (perhaps using Symfony2, CodeIgnitor, or your favorite framework). Maybe we have news, maybe we have announcements, and maybe we have videos. These three things. One some of them we can comment, and on all of them we are going to collect stats.

A basic schema could look like this:

Database Schema

The Rise of the DevOp

Here is a quick reference to what a DevOp is: http://en.wikipedia.org/wiki/DevOps

In the last couple of years a new development position within companies has emerged. The DevOp. The DevOp is a cross between a Developer or Engineer and TechOps. The person who holds the title DevOp should be able to be hired as Developer or as a TechOp, their skills in both areas must be very competent.

Recursive glob() VS. RecursiveDirectoryIterator

The glob() function has been around since PHP4 and it is a very widely used function. Below is an example of a basic recursive glob function as found on php.net.

I did take the liberty of renaming it to “rglob” I felt it was cleaner.

1
2
3
4
5
6
7
8
9
10
11
12
< ?php
 
// Does not support flag GLOB_BRACE
function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags); 
    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
    }
    return $files;
}
 
?>

While this is fine, you will discover that unless you had this handy function around already, you will end up searching for one, or creating one yourself. This problem was solved in PHP5 but is little known to many PHP developers. The solution: Iterators!

The New PHP 5.4

There is a new version of PHP has been released in the couple of weeks or so. PHP 5.4. I would like to highlight a few features that I am excited about. Here is a link to some basic new features from php.net.

 

Short Array Syntax

The new short array syntax is such a relief, I don’t know how many times I have typed array() and wished for the short Javascript like syntax. Maybe I’m just lazy but hey, that’s 5 extra characters that I’ve typed thousands of times, it adds up. Here are some examples:

1
2
$myArr = [1, 2, 3, 4 ,5];
$myArr = ['foo; => 'bar', 'test' => 99];

 

A Basic Database Connection Manager

The follow is an extremely simplified version of a database connection manager. It allows you to save multiple mysqli connections to make managing a complex environment more simple. There are many ways to implement a database connection manager this is probably one of the most generic and simple way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< ?php
    class DatabaseManager {
 
        private static $_instances = array();
 
        public static function getInstance($instance_name, $connection_info = array()) {
            if($connection_info) {
                list($db_host, $db_user, $db_pass, $db_name) = $connection_info;
                self::$_instances[$instance_name] = new MySQLi($db_host, $db_user, $db_pass, $db_name);
            }
            return self::$_instances[$instance_name];
        }
    }
 
    function db($instance_name='default', $connection_info = array()){
        return DatabaseManager::getInstance($instance_name, $connection_info);
    }
?>

Welcome

Welcome to my tech blog, I am going to spend the next couple of days cooking up some topics to write on, so stay tuned!

If you have any suggestions please let me know.

– Nathan Sepulveda

Subscribe to RSS Feed Subscribe to Tumblr Follow me on Twitter!