Convention Over Configuration

Also known as Coding by Convention.

The principle was created to keep unimportant decisions out of the way. Simple is better than complex, therefore you as a Developer can focus on writing “meaningful code”.

When you you declare a model on Rails, Laravel or Yii, The Framework, uses a convention to create the table for the given model.

In other words. You do not have to make a decision or configure the name for that table.

When I first started coding WPExpress I wanted it to be a tool to make my job easier. But more than that I wanted it to help me keep my code beautiful. This is the reason why starting today the BaseModel is adopting Convention Over Configuration.

Building WordPress Custom Post Types with WPExpress Before

Before I decided to implement  CoC you needed to code something like this to get your Custom Post Types off the ground.


<?php
namespace YourProject;
class Book extends BaseModel implements BaseModelInterface
{
public function __construct($bean = null)
{
parent::__construct($bean);
}
public function getPostType()
{
if( !isset(self::$postType) ){
static::$postType = 'post';
}
return static::$postType;
}
}

view raw

before-coc.php

hosted with ❤ by GitHub

This was horrible –I mean not as really horrible code goes but, it is not what I wanted–.

I found myself repeating to much code, which violated the DRY principle and also it was quite complex –Here I mean ugly–.

Building WordPress Custom Post Types with WPExpress now

I checked the codebase on a Laravel project I am writing. Their way to create models is beyond simple, beautiful.

I wanted something like that. So I decided to drop the Interface for good.

But that is not all. I went looking for a way to get the ShortName of the class through ReflectionClass. So now the only thing you need to create a Custom Post Type is to extend the BaseModel and instantiate it.


<?php
namespace YourProject;
class Book extends BaseModel
{
}

view raw

after-coc.php

hosted with ❤ by GitHub

That’s it. I am working on making WPExpress the WordPress Framework you want to use for your projects. I hope you like what you find here.

%d bloggers like this: