Take the 2-minute tour ×

I'm planning to write an e-commerce app using PHP & MySQL with lots & lots of custom rules. Say for example, I want to have a rule like "Give Customer X, a 10% a discount for Product Y if he bundles it with Product Z"

I've heard about Business rules engine(in Java and others). I intend to implement a flexible business rules functionality without hard-coding anything into PHP and with a good, solid architecture with the goal of maintainability and re-usability with the help of PHP OOPS.

Please advice me on the following aspects:

Does decision table fit my intended purpose?

How can I change rule/business logic of the PHP application without redeploying/re-writing?

share|improve this question

3 Answers 3

No matter whether you write the rules in PHP, JavaScript or some other, possibly domain-specific language. If the requirements are sufficiently complex (yours seem to be), they will require something with expressiveness of programming language and will require programming skill to update. Forget business people updating them, they don't have the necessary skill.

Now if programmer has to be around to update the rules, they are best written in existing language for which you can easily get somebody who understands it. For example PHP.

Now to the point of hard coding. PHP is not a compiled language. It is not even precompiled. The interpreter actually reparses it on each request. So when you change the file with business rules definition, it will immediately take effect. You want to version that file separately from the core of the application or you may even store it in the database, but PHP is probably simplest to implement, quite simple to maintain and also fastest as any rule interpreter written in PHP is going to add noticeable overhead.

You may want to use some sandbox (quick google reveals at least two implementations for PHP) to ensure that the business rules file can't touch anything outside the transaction object. This can be added anytime later, you can start without bothering about it. You may also later add some graphical language that will generate PHP into the database or something, but it's been repeatedly shown in practice that it takes a programmer to edit nevertheless, so it's effort with questionable return (though it might be a selling point).

(Edit) As I looked at decision tables, they don't seem to be very flexible. They need a column for every possible criterion, so as the rules grow in complexity, the table will need more and more columns and it won't be easy to understand any more anyway.

Also don't forget that an e-commerce package does not live in void, but needs to be integrated with various other systems. Most shops will probably already have a accounting system where they need to export data about finished transactions and store management system where they need to export data about goods to ship or in case of software shops accounts to create and licenses to assign, they will be using different payment processor to which you have to redirect and receive payment confirmation, etc. For these the logic will be different, so simple rules won't cut it. You absolutely need to support writing this in PHP, independent from the main core just like the business rules you mentioned.

share|improve this answer
Thanks for the advice. So should I use DB + Decision tables to implement biz rules or a PHP file, which is the better approach ? –  Mithun John Jacob Jul 24 '13 at 5:08
@MithunJohnJacob: I definitely recommend PHP snippets. Start with file though ultimately allowing to store it in database seems useful. –  Jan Hudec Jul 24 '13 at 5:55
@MithunJohnJacob: I also realized there is another kind of "business rules" than your example, the actual actions that needs to be done when deal is closed. These will be so vastly different between shops that you absolutely need full power of PHP for this. –  Jan Hudec Jul 24 '13 at 6:20
Thanks. You meant using stored procedures or triggers in MySQL ? –  Mithun John Jacob Jul 24 '13 at 6:59
@MithunJohnJacob: I didn't mean either. MySQL is not capable of executing PHP. I meant storing PHP snippets in database as simple text (CLOB, probably) data and evaling it instead of requiring files. –  Jan Hudec Jul 24 '13 at 7:30

I used to work with magento, which was an e-commerce platform (in php-mysql) that was doing exactly that. There is too-much effort for doing the functionality you want from scratch, so integrating an off-the-shelf solution might be more appropriate.

From the answer above: "Forget business people updating them, they don't have the necessary skill." This is timeless advice. People have tried it in the past, it always failed...

share|improve this answer
The last paragraph should have been a comment, probably. Indeed, I've read many tales of people trying that and failing spectacularly. –  Jan Hudec Jul 24 '13 at 9:05
@dimitris mistriotis: Thanks for the info. But, I'm not having any experience with Zend framework/Magento. I will look into Magento :-) –  Mithun John Jacob Jul 24 '13 at 9:35
Is it a better option to implement a plugin architecture using any of the design patters for biz rules ? –  Mithun John Jacob Jul 25 '13 at 6:08
I cannot get into your shoes and take decisions, but this might produce something easier to maintain. Usually in e-commerce we have to do 95% of things the "same" as everybody else for a consistent user experience and modify a small part where there is the need to innovate/apply a different rule etc. I believe that magento (for the hosted php word) has this right, same for other alternative options. I do not try to promote it, am not affiliated, do not use it now, just trying to state that in your case you would want to re-invent one wheel, not all of them. –  dimitris mistriotis Jul 25 '13 at 7:14

For my own purposes I considered https://github.com/bobthecow/Ruler - a production rules engine. It is applicable to your needs: Give Customer X, a 10% a discount for Product Y if he bundles it with Product Z.

I'll try to write some sort of a pseudo-code:

Context: your shopping cart items
Rules: cart contains ProductY and ProductZ
Execute: ProductY->discount(10%)
share|improve this answer
was going to answer with this. A rules engine should be the answer here. –  Adrian Schneider Jan 30 at 16:23

Your Answer


