The Code - The Model

Example 1.10. Model.inc.php
<?php
/**
 * Description of
 * File: Model.inc.php
 * 
 * The model in MVC looks after mod, management of data
 * it knows nothing about the controller or the view(s)
 *
 * @author nml
 * @version 2015-11-26, V0.9
 */

require_once 'models/DbH.inc.php';

class Model {
    private static $itemsarr;
    private static $dbh;
    
    public function __construct() {
        self::$itemsarr = array();
    }
    
    public static function getDbh() {
        self::$dbh = DbH::getDbH();
        return self::$dbh;
    }
    
    public function getItemsArr() {
        return self::$itemsarr;
    }
    public function setItemsArr($a) {
        self::$itemsarr = $a;
    }
}

Example 1.11. NewsItems.inc.php
<?php
require_once 'models/Model.inc.php';

/**
 * Description of NewsItem
 *
 * @author nml
 */

class NewsItem {
    private $id;
    private $tstamp;
    private $header;
    private $item;
    private $category;
    private $priority;
    
    public function __construct($id, $tstamp, $header, $item, $category, $priority) {
        $this->id = $id;
        $this->tstamp = $tstamp;
        $this->header = $header;
        $this->item = $item;
        $this->category = $category;
        $this->priority = $priority;
    }
    
    public function getNewsItem($id, $model) {
        $items = $model->getItemsArr();
        foreach ($items as $item) {
            if ($item->getId() === $id) {
                return $item;
            }
        }
        return NULL;
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function getWhen() {
        return substr($this->tstamp, 11, 5);
    }
    
    public function getHeader() {
        return $this->header;
    }
    
    public function getItem() {
        return $this->item;
    }
    
    public function getCategory() {
        return $this->category;
    }
    
    public function getPriority() {
        return $this->priority;
    }
    
    public function insert() {
        $sql = "insert into item (header, item, category, priority)";
        $sql .= " values(:header, :item, :cat, :pri);";
        try {
            $q = Model::getDbh()->prepare($sql);
            $q->bindValue(':header', $this->getHeader());
            $q->bindValue(':item', $this->getItem());
            $q->bindValue(':cat', $this->getCategory());
            $q->bindValue(':pri', $this->getPriority());
            $q->execute();
        } catch(PDOException $e) {
            printf("<p>Insert failed: <br/>%s</p>\n",
                $e->getMessage());
        }
    }
    
    public static function itemFactory ($model) {
        $arr = $model->getItemsArr();
        $sql = 'select *';
        $sql .= ' from item';
        try {
            $q = Model::getDbh()->prepare($sql);
            $q->execute();
            while ($row = $q->fetch()) {
                $item = new NewsItem($row['id']
                               , $row['tstamp']
                               , $row['header']
                               , $row['item']
                               , $row['category']
                               , $row['priority']);
                array_push($arr, $item);
            }
        } catch(PDOException $e) {
            printf("<p>Query failed: <br/>%s</p>\n",
                $e->getMessage());
        }
        return $arr;
    }

    public static function create() {
        $item = new NewsItem(null
                       , null
                       , $_POST['headline']
                       , $_POST['itemtext']
                       , $_POST['cat']
                       , $_POST['pri']);
        $item->insert();
    }
}