The best possible way to implement Business Rules in PHP

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?

1

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…

4

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.

6

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Context: your shopping cart items
Rules: cart contains ProductY and ProductZ
Execute: ProductY->discount(10%)
</code>
<code>Context: your shopping cart items Rules: cart contains ProductY and ProductZ Execute: ProductY->discount(10%) </code>
Context: your shopping cart items
Rules: cart contains ProductY and ProductZ
Execute: ProductY->discount(10%)

1

We just have released our Decision Tables Engine that can make decisions on data that you will send to it. Take a look, maybe it will fit your needs.

You simply need to send data to it, and to set-up set of rules (without coding) that will return decision to your product. Also you can re-use this rules in any other projects.

https://gndf.io/

I think right now its the only free decision engine with open code that is built on PHP. Other free alternative is Drools (works on Java JBoss) that was recommended in other answers.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

The best possible way to implement Business Rules in PHP

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?

1

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…

4

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.

6

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Context: your shopping cart items
Rules: cart contains ProductY and ProductZ
Execute: ProductY->discount(10%)
</code>
<code>Context: your shopping cart items Rules: cart contains ProductY and ProductZ Execute: ProductY->discount(10%) </code>
Context: your shopping cart items
Rules: cart contains ProductY and ProductZ
Execute: ProductY->discount(10%)

1

We just have released our Decision Tables Engine that can make decisions on data that you will send to it. Take a look, maybe it will fit your needs.

You simply need to send data to it, and to set-up set of rules (without coding) that will return decision to your product. Also you can re-use this rules in any other projects.

https://gndf.io/

I think right now its the only free decision engine with open code that is built on PHP. Other free alternative is Drools (works on Java JBoss) that was recommended in other answers.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật