<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                我把SPL分為五個部分:Iterator,Classes,Exceptions,Datastructures,Function;而其中classes是就是做一些類的介紹(Iterator與Datastructures相關的類在各自文章內),在介紹這些類之前,先介紹幾個接口: ## ArrayAccess(數組式訪問)接口 http://php.net/manual/zh/class.arrayaccess.php#class.arrayaccess 只要實現了這個接口,就可以使得object像array那樣操作。ArrayAccess界面包含四個必須部署的方法,這四個方法分別傳入的是array的key和value,: * offsetExists($offset) This method is used to tell php if there is a value for the key specified by offset. It should return true or false.檢查一個偏移位置是否存在 * offsetGet($offset) This method is used to return the value specified by the key offset.獲取一個偏移位置的值 * offsetSet($offset, $value) This method is used to set a value within the object, you can throw an exception from this function for a read-only collection. 獲取一個偏移位置的值 * offsetUnset($offset) This method is used when a value is removed from an array either through unset() or assigning the key a value of null. In the case of numerical arrays, this offset should not be deleted and the array should not be reindexed unless that is specifically the behavior you want. 復位一個偏移位置的值 ```php /** * A class that can be used like an array */ class Article implements ArrayAccess{ public $title; public $author; public $category; function __construct($title , $author , $category){ $this->title = $title; $this->author = $author; $this->category = $category; } /** * Defined by ArrayAccess interface * Set a value given it's key e.g. $A['title'] = 'foo'; * @param mixed key (string or integer) * @param mixed value * @return void */ function offsetSet($key , $value){ if (array_key_exists($key , get_object_vars($this))) { $this->{$key} = $value; } } /** * Defined by ArrayAccess interface * Return a value given it's key e.g. echo $A['title']; * @param mixed key (string or integer) * @return mixed value */ function offsetGet($key){ if (array_key_exists($key , get_object_vars($this))) { return $this->{$key}; } } /** * Defined by ArrayAccess interface * Unset a value by it's key e.g. unset($A['title']); * @param mixed key (string or integer) * @return void */ function offsetUnset($key){ if (array_key_exists($key , get_object_vars($this))) { unset($this->{$key}); } } /** * Defined by ArrayAccess interface * Check value exists, given it's key e.g. isset($A['title']) * @param mixed key (string or integer) * @return boolean */ function offsetExists($offset){ return array_key_exists($offset , get_object_vars($this)); } } // Create the object $A = new Article('SPL Rocks','Joe Bloggs', 'PHP'); // Check what it looks like echo 'Initial State:<div>'; print_r($A); echo '</div>'; // Change the title using array syntax $A['title'] = 'SPL _really_ rocks'; // Try setting a non existent property (ignored) $A['not found'] = 1; // Unset the author field unset($A['author']); // Check what it looks like again echo 'Final State:<div>'; print_r($A); echo '</div>'; ``` ## Serializable接口 **接口摘要** ```php Serializable { /* 方法 */ abstract public string serialize ( void ) abstract public mixed unserialize ( string $serialized ) } ``` 具體參考: http://php.net/manual/zh/class.serializable.php 簡單的說,當實現了Serializable接口的類,被實例化后的對象,在序列化或者反序列化時都會自動調用類中對應的序列化或者反序列化方法; ```php class obj implements Serializable { private $data; public function __construct() { $this->data = "自動調用了方法:"; } public function serialize() { $res = $this->data.__FUNCTION__; return serialize($res); } //然后上面serialize的值作為$data參數傳了進來; public function unserialize($data) { $this->data = unserialize($res); } public function getData() { return $this->data; } } $obj = new obj; $ser = serialize($obj); $newobj = unserialize($ser); //在調用getData方法之前其實隱式又調用了serialize與unserialize var_dump($newobj->getData()); ``` ## IteratorAggregate(聚合式迭代器)接口 **類摘要** ```php IteratorAggregate extends Traversable { /* 方法 */ abstract public Traversable getIterator ( void ) } ``` > Traversable作用為檢測一個類是否可以使用 foreach 進行遍歷的接口,在php代碼中不能用。只有內部的PHP類(用C寫的類)才可以直接實現Traversable接口;php代碼中使用Iterator或IteratorAggregate接口來實現遍歷。 實現了此接口的類,內部都有一個getIterator方法來獲取迭代器實例; ```php class myData implements IteratorAggregate { private $array = []; const TYPE_INDEXED = 1; const TYPE_ASSOCIATIVE = 2; public function __construct( array $data, $type = self::TYPE_INDEXED ) { reset($data); while( list($k, $v) = each($data) ) { $type == self::TYPE_INDEXED ? $this->array[] = $v : $this->array[$k] = $v; } } public function getIterator() { return new ArrayIterator($this->array); } } $obj = new myData(['one'=>'php','javascript','three'=>'c#','java',]/*,TYPE 1 or 2*/ ); //↓↓ 遍歷的時候其實就是遍歷getIterator中實例的迭代器對象,要迭代的數據為這里面傳進去的數據 foreach($obj as $key => $value) { var_dump($key, $value); echo PHP_EOL; } ``` ## Countable 接口 類實現 Countable接口后,在count時以接口中返回的值為準. ```php //Example One, BAD :( class CountMe{ protected $_myCount = 3; public function count(){ return $this->_myCount; } } $countable = new CountMe(); echo count($countable); //result is "1", not as expected //Example Two, GOOD :) class CountMe implements Countable{ protected $_myCount = 3; public function count(){ return $this->_myCount; } } $countable = new CountMe(); echo count($countable); //result is "3" as expected ``` ## ArrayObject類 簡單的說該類可以使得像操作Object那樣操作Array; > 這是一個很有用的類; ```php /*** a simple array ***/ $array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus'); /*** create the array object ***/ $arrayObj = new ArrayObject($array); //增加一個元素 $arrayObj->append('dingo'); //顯示元素的數量 //echo $arrayObj->count(); //對元素排序: 大小寫不敏感的自然排序法,其他排序法可以參考手冊 $arrayObj->natcasesort(); //傳入其元素索引,從而刪除一個元素 $arrayObj->offsetUnset(5); //傳入某一元素索引,檢測某一個元素是否存在 if ($arrayObj->offsetExists(5)) { echo 'Offset Exists<br />'; } //更改某個元素的值 $arrayObj->offsetSet(3, "pater"); //顯示某一元素的值 //echo $arrayObj->offsetGet(4); //更換數組,更換后就以此數組為對象 $fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10); $arrayObj->exchangeArray($fruits); // Creates a copy of the ArrayObject. $copy = $fruitsArrayObject->getArrayCopy(); /*** iterate over the array ***/ for($iterator = $arrayObj->getIterator(); /*** check if valid ***/ $iterator->valid(); /*** move to the next array member ***/ $iterator->next()) { /*** output the key and current array value ***/ echo $iterator->key() . ' => ' . $iterator->current() . '<br />'; } ``` ## SplObserver, SplSubject 這是兩個專用于設計模式中觀察者模式的類,會在后面的設計模式專題中詳細介紹; ## SplFileInfo 簡單的說,該對象就是把一些常用的文件信息函數進行了封裝,比如獲取文件所屬,權限,時間等等信息,具體參考: http://php.net/manual/zh/class.splfileinfo.php ## SplFileObject SplFileObject類為操作文件提供了一個面向對象接口. 具體參考:http://php.net/manual/zh/class.splfileobject.php ```php SplFileObject extends SplFileInfo implements RecursiveIterator , SeekableIterator {} ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看