SingleObjectClass.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yckj_lzj
  5. * Date: 2019-08-21
  6. * Time: 16:38
  7. */
  8. namespace app\common\util;
  9. use think\Log;
  10. class SingleObjectClass
  11. {
  12. protected $openLog=true;
  13. //输出结果
  14. protected function writeln($info, $type = "debug")
  15. {
  16. if ($this->openLog) {
  17. Log::write(print_r($info, true), $type);
  18. }
  19. }
  20. /**
  21. * 返回内容
  22. * @param $code
  23. * @param $data
  24. */
  25. protected function getResult($code, $data = null)
  26. {
  27. $result['code'] = $code;
  28. $result['msg'] = ActQuestionErrCode::$errInfo[$code];
  29. if ($data) {
  30. $result['data'] = $data;
  31. }
  32. return $result;
  33. }
  34. /**
  35. * 类实例化(单例模式)
  36. */
  37. public static function instance()
  38. {
  39. static $_instance = array();
  40. $classFullName = get_called_class();
  41. if (!isset($_instance[$classFullName])) {
  42. // $_instance[$classFullName] = new $classFullName();
  43. // 1、先前这样写的话,PhpStrom 代码提示功能失效;
  44. // 2、并且中间变量不能是 数组,如 不能用 return $_instance[$classFullName] 形式返回实例对象,否则 PhpStrom 代码提示功能失效;
  45. $instance = $_instance[$classFullName] = new static();
  46. return $instance;
  47. }
  48. return $_instance[$classFullName];
  49. }
  50. }