Model - kouji6309/SingleMVC GitHub Wiki

說明

模型的基底類別。


原型

abstract class Model {
    /* 屬性 */
    protected static $db_pdo : PDO
    protected $db_statement : PDOStatement
    
    /* 方法 */
    protected db_connect(array $config = null) : bool
    protected db_debug() : string
    protected db_begin() : bool
    protected db_commit() : bool
    protected db_rollBack() : bool
    protected db_query(string $statement) : PDOStatement
    protected db_prepare(string $statement) : PDOStatement
    protected db_insert() : mixed
    protected db_select(bool $force_array = false) : array
    protected db_update() : int
    protected db_bind(
        mixed $parameter,
        mixed $value = '',
        int $type = PDO::PARAM_STR
    ) : bool

    protected static password_hash(string $password) : string
    protected static password_verify(string $password, string $hash) : bool

    protected static request(
        string $url,
        string $method = 'get',
        mixed $data = [],
        array $option = [],
        bool $get_header = false
    ) : mixed
    protected static request_async(
        string $url,
        string $method = 'get',
        mixed $data = [],
        array $option = []
    ) : CurlHandle
    protected static request_run(
        mixed $rs,
        int $start = 0,
        int $length = -1,
        bool $get_header = false
    ) : mixed
    protected static request_parse(string $response) : array
}

屬性

  • PDO 物件。框架中所有繼承 Model 的物件將共用連線。

  • 最後的 PDO 敘述。每個繼承 Model 的物件各自擁有。

方法

 

 


應用

建立模型,並使用 SingleMVC 提供的功能

class News extends Model {
    public function getNews($id) {
        $r = self::request('https://www.example.com/api/news', 'get', ['id' => $id]);
        $r = json_decode($r, true);
        return $r['data'] ?? 'error';
    }
}
⚠️ **GitHub.com Fallback** ⚠️