DEV Community

PHP and cigars
PHP and cigars

Posted on

Why I chose to create my own PHP7 shop-software (and I am happy with it)

Disclaimer

Please excuse my poor english. I had to quit taking english classes in highschool because of an really bad teacher.

About my project

Two years ago i started a new job. My employeer wanted me to create a new online-shop and gave me total freedom. As it is a side project for the company and mainly serves to explore new technology, ux and seo methods and has only to be profitable on the long term, I had the opportunity to create a nice piece of software and learn a lot during the way. I do not say the path I took was the right one, but it seemed to be the right one for me.

Requirements

Create a fast, scalable, extendable multi-shop-software.

First decision: Language

Yes, I was curious to try something new to me like Ruby on Rails or Django, but in the end it seemed a too big project for me to fail. I've seen projects fail because whole teams switched to rails without having experience with the language. So I sticked with PHP, that I first tried in 2001. I also was curious to try the new features of PHP7.

Second decision: Shop-software, PHP-framework or start (almost) from scratch?

The goal was to create a fast loading, nice to customize web-shop. I first thought about trying Magento. A lot of competitors use Magento, so how could I create an advantage with the same software? At the end the slow speed was a weak point where I could gain points. I tried some others, but was not satisfied either. Today I would probably decide to try shopware. But the version at that time did not fit my needs.

Also my experience with frameworks was not too good in the past. Maybe I got in contact with Symfony and CakePHP too early. I thought, that i spend to much time at looking things up and was often disappointed, when features where missing.

So, i decided to do something stupid and create a new shop based on a conference talk. I had written several MVC-Frameworks before. The last time I wrote an framework was for my bachelor-thesis. So I decided to strip the basic object, list and datatype classes out and use them for my project.

The mentioned conference talk was about creating a push-architecture instead of a pull architecture, where almost everything is static, cached content.

Splitting everything up in App/Core

As I wanted to reuse code and occasionally overwrite codes to customize code for a specific shop, I decided to adress everything over the App Namespace. So I have a lot of empty classes, that just extend the Core-classes.

A usefully tool was the "Kaderschule" (cadre school). As I share my Surname with a socialist head of state I thought it was funny to make a pun of it and name the class creator skript like that. That script creates classes not only for one project, but optionaly for all projects I work with only one command.

Template-Engine vs. Renderer

As I wanted to do all the fancy stuff that people are used theese days I thought that it would be a good idea to try out an template engine. Unfortunatly the possibility to overwrite parts did not seem very elegant. I will explain this in detail soon.

So I did some research. I even went to the university library. All books (like 7 or 8 books) that had somehow web-development as a topic suggested the same: Use a Template-Engine.

At the end I came up with an listing in Clean Code by Uncle Bob Martin. He used several renderer-classes to replace an output.
Also it means that I have to write more code, I'm satified with the way I can navigate through an renderer and how i can overwrite and extend renderer-classes.

Consider this listing:

public function Render() {
    return
        $this->RenderProductName()
        . $this->RenderProductImage()
        . $this->RenderPrice()
        . $this->RenderAddToShoppingCartButton()
        ...
    ;
}

Remember that I want to create a code-base for several shops. The above function could be in an core-class ProductRenderer. Imagine, that for one of n shops i want to render a short summary of the user-reviews (a.k.a. stars) beetween the price and the add to shopping-cart-button.

I can simply change the code to

...
        . $this->RenderPrice()
        . $this->RenderRatingssummary() // New function was inserted here
        . $this->RenderAddToShoppingCartButton()
...

    protected function RenderRatingssummary() {
        return '';
    }

and than I can implement the function in the corresponding class in the App Namespace that extends the core class. Using a template engine I would have to copy /paste this section. Even in a shop-software were you are not supposed to touch the core classes i would have to copy/paste that code. That is something I really needed to avoid.

So I´m very satisfied with this solution. Maybe I will still use an template engine to create some static domain specific content in the future, but for now that case never came. I really envy the readability of HAML.

Trigger pages to be pre-rendered.

As mentioned, I wanted to create a super fast website. So every product, brand, category-page and so on is prerendered and cached in a memcache-storage. The memcache-entries contain the HTML that is replaced by user specific content like the shopping cart counter or the logged in icon. After the replacement the HTML can just be echoed.

Everytime a product changes, the corresponding pages have to be re-rendered. That is archieved by creating Trigger, that just insert an object id and the object-type to an render fifo.

A daemon checks regualary if there is data in the render fifo and then beginns to render pages and store the output of the renderers in the cache. That is actually not that complicated as if first seemed, but still created some mental friction, so I asked an old study-mate to help me with that part. In the end cleaning up some code helped me creating the main trick to create a fast online-shop.

Conclusion

I do not say, stop using that framework or that shop-software. I just can say, that for me and my needs it was the wright decision. And if you want to try that too: I´m not a genius. I´m sure if you have the budget (I think that is the biggest problem in most companies) to write an own shop-software you can do it, too.

I hope this article helped to encourage someone to just loose a little bit the fear of creating something similar and to make clear that software is not magic. It´s simply hours and hours of coding.

Oldest comments (0)