Think.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP系统基类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Core
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Think
  24. {
  25. private static $_instance = array();
  26. /**
  27. +----------------------------------------------------------
  28. * 自动变量设置
  29. +----------------------------------------------------------
  30. * @access public
  31. +----------------------------------------------------------
  32. * @param $name 属性名称
  33. * @param $value 属性值
  34. +----------------------------------------------------------
  35. */
  36. public function __set($name ,$value)
  37. {
  38. if(property_exists($this,$name))
  39. $this->$name = $value;
  40. }
  41. /**
  42. +----------------------------------------------------------
  43. * 自动变量获取
  44. +----------------------------------------------------------
  45. * @access public
  46. +----------------------------------------------------------
  47. * @param $name 属性名称
  48. +----------------------------------------------------------
  49. * @return mixed
  50. +----------------------------------------------------------
  51. */
  52. public function __get($name)
  53. {
  54. return isset($this->$name)?$this->$name:null;
  55. }
  56. /**
  57. +----------------------------------------------------------
  58. * 系统自动加载ThinkPHP类库
  59. * 并且支持配置自动加载路径
  60. +----------------------------------------------------------
  61. * @param string $classname 对象类名
  62. +----------------------------------------------------------
  63. * @return void
  64. +----------------------------------------------------------
  65. */
  66. public static function autoload($classname)
  67. {
  68. // 检查是否存在别名定义
  69. if(alias_import($classname)) return ;
  70. // 自动加载当前项目的Actioon类和Model类
  71. if(substr($classname,-5)=="Model") {
  72. require_cache(LIB_PATH.'Model/'.$classname.'.class.php');
  73. }elseif(substr($classname,-6)=="Action"){
  74. require_cache(LIB_PATH.'Action/'.$classname.'.class.php');
  75. }else {
  76. // 根据自动加载路径设置进行尝试搜索
  77. if(C('APP_AUTOLOAD_PATH')) {
  78. $paths = explode(',',C('APP_AUTOLOAD_PATH'));
  79. foreach ($paths as $path){
  80. if(import($path.$classname))
  81. // 如果加载类成功则返回
  82. return ;
  83. }
  84. }
  85. }
  86. return ;
  87. }
  88. /**
  89. +----------------------------------------------------------
  90. * 取得对象实例 支持调用类的静态方法
  91. +----------------------------------------------------------
  92. * @param string $class 对象类名
  93. * @param string $method 类的静态方法名
  94. +----------------------------------------------------------
  95. * @return object
  96. +----------------------------------------------------------
  97. */
  98. static public function instance($class,$method='')
  99. {
  100. $identify = $class.$method;
  101. if(!isset(self::$_instance[$identify])) {
  102. if(class_exists($class)){
  103. $o = new $class();
  104. if(!empty($method) && method_exists($o,$method))
  105. self::$_instance[$identify] = call_user_func_array(array(&$o, $method));
  106. else
  107. self::$_instance[$identify] = $o;
  108. }
  109. else
  110. halt(L('_CLASS_NOT_EXIST_'));
  111. }
  112. return self::$_instance[$identify];
  113. }
  114. }//类定义结束
  115. ?>