<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 SPL是Standard PHP Library(PHP標準庫)的縮寫。 根據官方定義,它是"a collection of interfaces and classes that are meant to solve standard problems" **SPL是用于解決典型問題(standard problems)的一組接口與類的集合。** 但是,目前在使用中,SPL更多地被看作是一種使object(物體)模仿array(數組)行為的interfaces和classes。SPL的核心概念就是Iterator。 在我的理解中,SPL以及后面要說的設計模式專題都是用于同一個目的: **構建優雅簡潔易于擴展和維護的代碼**,有時候我們看上去寫了更多的代碼,但事實上卻讓代碼的擴展性和維護性變得更強。 > 另外本專題屬于PHP進階課程。在本專題中給出的一些Example,看上去是有更簡單的替代方案,但在實際上在更復雜的開發中,看似更多的代碼卻使得程序的可插拔性,可維護性變得更強,SPL以及設計模式都算是面向對象中的精髓了,所以面向對象的基礎一定要掌握得非常牢才更能理解; ## Iterator 迭代器有時又稱光標(cursor)是程式設計的軟件設計模式,可在容器物件(container,例如list或vector)上遍訪的接口,設計人員無需關心容器物件的內容。 PHP5開始支持了接口, 并且內置了Iterator接口, 所以如果你定義了一個類,并實現了Iterator接口,那么你的這個類對象就是ZEND_ITER_OBJECT,否則就是ZEND_ITER_PLAIN_OBJECT.對于ZEND_ITER_PLAIN_OBJECT的類,foreach會通過HASH_OF獲取該對象的默認屬性數組,然后對該數組進行foreach。 而對于ZEND_ITER_OBJECT的類對象,則會通過調用對象實現的Iterator接口相關函數來進行foreach。 通俗地說,Iterator能夠使許多不同的數據結構,都能有統一的操作界面,比如一個數據庫的結果集、同一個目錄中的文件集、或者一個文本中每一行構成的集合。 如果按照普通情況,遍歷一個MySQL的結果集,程序需要這樣寫: ``` // Fetch the "aggregate structure" $result = mysql_query("SELECT * FROM users"); // Iterate over the structure while ( $row = mysql_fetch_array($result) ) { // do stuff with the row here } ``` 讀出一個目錄中的內容,需要這樣寫: ``` // Fetch the "aggregate structure" $dh = opendir('/home/harryf/files'); // Iterate over the structure while ( $file = readdir($dh) ) { // do stuff with the file here } ``` 讀出一個文本文件的內容,需要這樣寫: ``` // Fetch the "aggregate structure" $fh = fopen("/home/hfuecks/files/results.txt", "r"); // Iterate over the structure while (!feof($fh)) { $line = fgets($fh); // do stuff with the line here } ``` 上面三段代碼,雖然處理的是不同的resource(資源),但是功能都是遍歷結果集(loop over contents),因此Iterator的基本思想,就是將這三種不同的操作統一起來,用同樣的命令界面,處理不同的資源。 SPL提供了6個迭代器接口(具體的會在本專題后續的文章里面說明): | 名稱 | 功能 | | :-------- | :----- | | Traversable | 遍歷接口(檢測一個類是否可以使用 foreach 進行遍歷的接口) | | Iterator | 迭代器接口(可在內部迭代自己的外部迭代器或類的接口) | | IteratorAggregate | 聚合式迭代器接口(創建外部迭代器的接口) | | OuterIterator| 迭代器嵌套接口(將一個或多個迭代器包裹在另一個迭代器中) | | RecursiveIterator | 遞歸迭代訪問接口(提供遞歸訪問功能) | | SeekableIterator | 可索引迭代訪問接口(實現查找功能) | ## Classes SPL除了定義一系列Interfaces以外,還提供一系列的內置類,它們對應不同的任務,大大簡化了編程。 查看所有的內置類,可以使用下面的代碼: ``` // a simple foreach() to traverse the SPL class names foreach(spl_classes() as $key=>$value){ echo $key.' -&gt; '.$value.'<br />'; } ``` ## Datastructures 同時 SPL 還提供了些數據結構基本類型的實現 。雖然我們可以使用傳統的變量類型來描述數據結構,例如用數組來描述堆棧(Strack)-- 然后使用對應的方式 pop 和 push(arraypop()、arraypush()),但你得時刻小心,·因為畢竟它們不是專門用于描述數據結構的 -- 一次誤操作就有可能破壞該堆棧。 而 SPL 的 SplStack 對象則嚴格以堆棧的形式描述數據,并提供對應的方法。同時,這樣的代碼應該也能理解它在操作堆棧而非某個數組,從而能讓你的同伴更好的理解相應的代碼,并且它更快。 SPL擁有的以下數據結構: Doubly Linked Lists(雙向鏈表),Heaps(堆),Arrays(陣列),Map(映射) ## Function 同時SPL還提供了很多方便的函數,比如我們在框架中經常用到的`spl_autoload_register`(注冊給定的函數作為 __autoload 的實現) - class_implements — 返回指定的類實現的所有接口。 - class_parents — 返回指定類的父類。 - class_uses — Return the traits used by the given class - iterator_apply — 為迭代器中每個元素調用一個用戶自定義函數 - iterator_count — 計算迭代器中元素的個數 - iterator_to_array — 將迭代器中的元素拷貝到數組 - spl_autoload_call — 嘗試調用所有已注冊的__autoload()函數來裝載請求類 - spl_autoload_extensions — 注冊并返回spl_autoload函數使用的默認文件擴展名。 - spl_autoload_functions — 返回所有已注冊的__autoload()函數。 - spl_autoload_register — 注冊給定的函數作為 __autoload 的實現 - spl_autoload_unregister — 注銷已注冊的__autoload()函數 - spl_autoload — __autoload()函數的默認實現 - spl_classes — 返回所有可用的SPL類 - spl_object_hash — 返回指定對象的hash id
                  <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>

                              哎呀哎呀视频在线观看