class self_test { const PC = 'pc'; const SP = 'sp'; private $_splist = array('iPhone','Android','iPod','iPad','Tizen');//设置经常使用的sp终端,暂时常用的sp端就这几种,如果有的话大家也可以增加 private $terminal; public function __construct(){ $this->setTerminal();//通过setTerminal()方法获取到$terminal变量的值 } /* * function setTerminal() * 获取终端信息 * @return string */ private function setTerminal(){ $isSp = false; foreach($this->_splist as $spname){ if (strstr($_SERVER['HTTP_USER_AGENT'], $spname)) { $isSp = true; break; } } return $this->terminal = ($isSp) ? self::SP : self::PC; } /* * function PC_SP() * 输出终端信息 * @return string */ public function PC_SP(){ return $this->terminal; } } $str = new self_test(); echo $str->PC_SP();//输出目前客户使用的是PC还是SP