ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1.ArrayAccess ArrayAccess 是PHP标准库(SPL)提供的一个接口,这意味着我们可以直接调用,该接口使得对对象的访问像数组一样 ``` <?php 2 class Test implements ArrayAccess 3 { 4 private $testData; 5 6 public function offsetExists($key) 7 { 8 return isset($this->testData[$key]); 9 } 10 11 public function offsetSet($key, $value) 12 { 13 $this->testData[$key] = $value; 14 } 15 16 public function offsetGet($key) 17 { 18 return $this->testData[$key]; 19 } 20 21 public function offsetUnset($key) 22 { 23 unset($this->testData[$key]); 24 } 25 } 26 27 $obj = new Test(); 28 29 //自动调用offsetSet方法 30 $obj['data'] = 'data'; 31 32 //自动调用offsetExists 33 if(isset($obj['data'])){ 34 echo 'has setting!'; 35 } 36 //自动调用offsetGet 37 var_dump($obj['data']); 38 39 //自动调用offsetUnset 40 unset($obj['data']); 41 var_dump($test['data']); 42 43 //输出: 44 //has setting! 45 //data 46 //null ``` 2.# IteratorAggregate聚合式迭代器 ``` <?php /** * 利用聚合式迭代器,并返回一个实现了Iterator接口的类的实例 * * @author 疯狂老司机 */ class myData implements IteratorAggregate { public $one = "Public property one"; public $two = "Public property two"; public $three = "Public property three"; public function __construct() { $this->last = "last property"; } public function getIterator() { return new ArrayIterator($this); } } $obj = new myData; foreach($obj as $key => $value) { var_dump($key, $value); echo '<br>';// Linux:echo "\n"; } 以上例子输出: string 'one' (length=3) string 'Public property one' (length=19) string 'two' (length=3) string 'Public property two' (length=19) string 'three' (length=5) string 'Public property three' (length=21) string 'last' (length=4) string 'last property' (length=13) ``` 3.# ArrayIterator迭代器遍历数组 ``` <?php namespace app\index\controller; use ArrayObject;//引入迭代器 class Index { public function index() { $fruits = array( "apple" => 'apple value',//position =0 "orange" => 'orange value',//position =1 "grape" => 'grape value', "plum" => 'plum value', ); dump($fruits); echo '------------------普通数组遍历-----------------'."<br/>"; foreach ($fruits as $key => $value) { echo $key.":".$value."<br/>"; } echo '------------------使用ArrayIterator迭代器遍历数组(foreach)-----------------'."<br/>"; $obj = new ArrayObject($fruits);//创建数组对象 $it = $obj->getIterator();//获取迭代器 foreach ($it as $key => $value) { echo $key.":".$value."<br/>"; } ``` 4.继承Countable接口的可被用于count() 函数。 ``` <?php class Basket implements Countable{ private $fruits =array('apple','banna','pear','orange','watermelon'); public function count(){ return count($this->fruits); } } $basket = new Basket(); var_dump(count($basket)); ``` 5.# PHP 异常与错误 —— InvalidArgumentException ~~~ class InvalidArgumentException extends LogicException implements Throwable{ /* 继承方法 */ final public string Exception::getMessage ( void ) // 获取抛出的消息内容 final public Throwable Exception::getPrevious ( void ) // 返回上一个 Throwable final public mixed Exception::getCode ( void ) // 获取抛出的错误代码 final public string Exception::getFile ( void ) // 获取产生异常的文件名 final public int Exception::getLine ( void ) // 获取相关行号 final public array Exception::getTrace ( void ) // 获取追踪信息,返回数组形式 final public string Exception::getTraceAsString ( void ) // 获取追踪信息,返回字符串形式 public string Exception::__toString ( void ) // 抛出的对象以字符串形式返回,可以用 echo 打印相应结果 final private void Exception::__clone ( void ) // 尝试克隆异常,将导致一个致命错误 } ~~~