How I ended up making a PHP framework

I used to talk very rarely about my works. But I'm making games and some other things that not a lot of people do, so maybe I have some interesting things to share, maybe ;)
You read it in my title, I made a php framework. But at the beginning I wanted to make an iOS game! So what happens? A lot of things.
My first iOS App
As I said, I wanted to make an iOS game, just for fun and because I like programming and designing games. So I bought an iMac, installed Xcode and started to learn Objective-C. The first things I've done is a lot of tutorials. The best website for that is Ray Wenderlich. There's many tutorials about a lots of things.
I learnt programming at school. At that time it was not C or C++, but Turbo Pascal (It was only for introduction). And I always programed a little bit in professional or personal projects (lua, actionscript, php). But, I never programmed in oriented object. And, as you know, Objective-C is an oriented object language. So it was the right time to learn OO programming. I was excited! But it was more difficult than I thought. After more than 10 years of "classic" programming, I needed to change my way of visualize the logic of a program, and I spent more time on tutorials than I thought.
Finally, I made an app. I simple one that display the last news, videos and podcasts of a website (soniconline.fr) from a json file. I also made a custom image gallery to view the news' pictures in the best way possible. This app has never been published, but was running great on my iphone 3GS ;) Maybe I'll published it one day.


From iOS to PHP
So I was ready to make my first iOS game! But, to be honest, I thought I needed more practice to master the OO programming. So I had an idea (damn me). I have that website, SonicOnline.fr, created in July 2000 by myself. The last version of the site was 3 years old, in basic php with lots of functions everywhere (it was a mess to be honest), so I decided to renovate it in oriented object php. I thought it would be an interesting exercise. And it was also a good opportunity to add some trendy features web 2.0 gamification-like stuffs. And, I thought it would be easy :)

But in fact it took me one year (I worked only during my days off and week-ends) to build that website and realizing that I made a full PHP framework. But I learnt a lot of things and best practices. I will try to remember a few ones.

Don't underestimate the importance of your models & managers
The best way to build a website is to adopt the MVC (model-view-controller) structure: managers will get the data (models) from the database (MySQL in my case), Controllers will retrieve the right data depending on the user's current page url, and the view part is handled by a template system (I'm using Smarty, a great simple template system) that display in html & css the data send by the controllers.

So the manager is the class object that will retrieve the data from the Database. At first it seems easy because you just need to create an object that will create a database query and return the data in one or several objects. But If you want your manager to be efficient, you'll need to add more features/options.
The first one is the ability to retrieve associated models for a specific model. For instance, your website has some news. But for each news you have related informations like the author, the related games, the comments, etc. All these elements are stored in your database, and it would be great to retrieve all these informations in a single query with your news. This is called "relations" between data objects. A relation can be one-to-one, one-to-many, many-to-many, and must be defined in each of your news, users, etc. To retrieve the relations of a single model you will need to build a request that uses Joins.
Your pages' controllers will get the data by using the managers. A good thing is to make the syntax the simplest possible (less is more!) For instance my syntax to get the last 5 news is:

Responsive image
With that syntax I get an array with the last 5 news, with, for each news, the author and the board topic used for the comments.

It's important to add in your find method some options to optimize the query and getting only the informations you need to display on your page, like the relations you need with each of your retrieved objects.

A CSS sample page
If the design of your website is wisely done, you use plenty of common styles in each of your pages. To check that all your css are still correct after modifications and to help you build new template pages, it's interesting to create an html page that repertory all your common styles and the html code used for them. It really saves some times.

A great php, javascript & css editor
I work on Mac and I'm using Coda to code my php, css & javascript files. I think it's the best editor for that on Mac with lots of useful features. The only missing feature is the word completion for method names. If you know an editor that can do this, feel free to give me its name in the comments.

Don't build your own PHP framework
It can be strange for me to say that, but if you want to build a website from scratch I recommend that you use an open source php framework. Because building your own complete framework will take you a lot of time, and there's plenty of good ones like CakePhp. If I knew how long it would be to make my own one, I surely would have chosen an existing framework.

What's next now?
Well, I don't really know what I will do next. I always have lots of ideas in my head, I just need to choose which one I will do first.
For sure, I can say now that I know how to program in oriented object, and it will surely help me in my game designer work to know when a programmer try to bullshit me ;)
I could also make a simple iOS game, like Flappy Bird with the hope to be rich ;)
And I would like to test some gamification features on the SonicOnline.fr's users. Social is today a big element in video games and I would like to experiment some things.

I want to record some music too, but that's another story...

PS: More infos about SonicOnline.fr

Leave a comment