23 教你正确使用__callStatic的技巧
代码
<?php
abstract class Query
{
abstract protected function record ( array $data ) ;
public function select ( )
{
$this -> record ( [ 'name' => '后盾人' , 'age' => 21 , 'tel' => '19999999999' ] ) ;
}
}
class Model extends Query
{
protected $field = [ ] ;
protected $deny = [ ] ;
public function all ( )
{
$this -> select ( ) ;
return $this -> field ;
}
protected function record ( array $data )
{
$this -> field = $data ;
}
protected function __tel ( )
{
return substr ( $this -> field [ 'tel' ] , 0 , 8 ) . '***' ;
}
public function __get ( $name )
{
if ( method_exists ( $this , '__' . $name ) ) {
return call_user_func_array ( [ $this , '__' . $name ] , [ ] ) ;
}
if ( isset ( $this -> field [ $name ] ) ) {
return $this -> field [ $name ] ;
}
throw new Exception ( 'arg error!' ) ;
}
public function __set ( $name , $value ) {
if ( isset ( $this -> field [ $name ] ) ) {
$this -> field [ $name ] = $value ;
}
else {
throw new Exception ( 'arg error!' ) ;
}
}
public function __unset ( $name )
{
if ( ! isset ( $this -> field [ $name ] ) || in_array ( $name , $this -> deny ) ) {
throw new Exception ( '属性不存在或禁止操作' ) ;
return true ;
}
$this -> field [ $name ] = '' ;
}
public function __isset ( $name )
{
return isset ( $this -> field [ $name ] ) ;
}
public function __call ( $name , $args ) {
$action = 'getAttribute' . $name ;
if ( method_exists ( $this , $action ) ) {
return call_user_func_array ( [ $this , $action ] , $args ) ;
}
}
public static function __callStatic ( $name , $args )
{
switch ( $name ) {
case 'getAll' :
return call_user_func_array ( [ new static ( ) , 'all' ] , $args ) ;
break ;
default :
$obj = new static ( ) ;
$obj -> all ( ) ;
return call_user_func ( [ $obj , '__call' ] , $name , $args ) ;
}
}
}
class user extends Model
{
protected function getAttributetel ( int $len = 9 )
{
return substr ( $this -> field [ 'tel' ] , 0 , $len ) . '***' ;
}
}
try {
echo user :: tel ( ) ;
} catch ( Exception $e ) {
echo $e -> getMessage ( ) ;
}