Zie deze code in actie bij de snippets.

De class/code die een Json file gaat uitlezen.

    <?php
    class json_oop {

        public function __construct($json_cat) {
        $this -> mc_json($json_cat);
        }

        public function mc_json($json_cat) {
        $jsondata = file_get_contents("json/elements.json");
        $json = json_decode($jsondata, true);
        $output = " <ul >";

        foreach ($json["$json_cat"] as $links) {
        $output .= " <li >";
        $output .= "<a href'javascript:void(0)' class='" . $links["effect"] . "' data-hover='" . $links["Name"] . "'>" . $links["Name"];
        $output .= "<span >" . $links["subtext"] . "</span >";
        $output .= "</a>";
        $output .= "</li >
        ";
        }

        $output .= " </ul > ";
        echo $output;
        }
    }
    ?>
    
De json file die wordt uitgelezen.

    {
    "buttons":[
        {"Name":"button one", "effect":"btn btn_1","subtext":""},
        {"Name":"button two", "effect":"btn btn_2","subtext":""},
        {"Name":"button three", "effect":"btn btn_3","subtext":""},
        {"Name":"button four", "effect":"btn btn_4","subtext":""}
    ],
    "links":[
        {"Name":"brackets", "effect":"link_effect_1","subtext":""},
        {"Name":"border_up", "effect":"link_effect_2","subtext":""},
        {"Name":"border down", "effect":"link_effect_3","subtext":""},
        {"Name":"filling", "effect":"link_effect_5","subtext":""},
        {"Name":"Dots", "effect":"link_effect_6","subtext":""},
        {"Name":"border and subtext", "effect":"link_effect_4","subtext":"this one has subtext"}
    ]
    }
        
Hoe de class wordt aangeroepen.

    <?php
    $buttons = new json_oop('buttons');
    ?>

    of

    <?php
    $buttons = new json_oop('links');
    ?>
        
Zie deze code in actie bij de snippets.