PHP code example of nece001 / php-decision-table

1. Go to this page and download the library: Download nece001/php-decision-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nece001 / php-decision-table example snippets




// 定义一个用来决定下雨是否出门的决策表
class RainTable extends DecisionTableAbstract
{
    protected function buildTable()
    {
        // 条件值并不局限于0和1,可以用“===”比较的都可以
        $this->addRule(array('下雨' => 1, '有车' => 1, '有事' => 1), '出门');
        $this->addRule(array('下雨' => 0, '有车' => 0, '有事' => 0), '呆家');

        $this->addRule(array('下雨' => 1, '有车' => 1, '有事' => 0), '呆家');
        $this->addRule(array('下雨' => 1, '有车' => 0, '有事' => 0), '呆家');
        $this->addRule(array('下雨' => 1, '有车' => 0, '有事' => 1), '出门');

        $this->addRule(array('下雨' => 0, '有车' => 1, '有事' => 1), '出门');
        $this->addRule(array('下雨' => 0, '有车' => 0, '有事' => 1), '出门');
        $this->addRule(array('下雨' => 0, '有车' => 1, '有事' => 0), '呆家');
    }
}

$r = new RainTable();
echo $r->decide(array('下雨' => 1, '有车' => 1, '有事' => 1)); // 输出:出门
echo $r->decide(array('有车' => 0, '有事' => 0, '下雨' => 1)); // 输出:呆家