<?php
/**
* Model 基类
* Class BaseModel
*/
class BaseModel
{
/**
* @var static
*/
protected static $instance;
/**
* @var int 错误代号
*/
protected $errCode;
/**
* 获取当前类
* @return static
*/
public static function getInstance()
{
if (isset(self::$instance[get_called_class()]) && self::$instance[get_called_class()] instanceof static) {
return self::$instance[get_called_class()];
}
return self::$instance[get_called_class()] = new static();
}
/**
* 获取报错代号
* @return int
*/
public function getErrCode()
{
return $this->errCode ? $this->errCode : -100;
}
/**
* 获取错误信息
* @return string
*/
public function getErrMessage()
{
return $this->errMessage ? $this->errMessage : '没有指定错误信息';
}
}
每个 model 类继承 BaseModel 即可
class TestModel extends BaseGame
{
public function test
{
echo 'test';
}
}
使用
TestModel::getInstance()->test();
TestModel::getInstance()->test();
TestModel::getInstance()->test();
不管执行多少此,都是只实例化一次
网友评论