php面向對象高級-魔術方法與迭代器

来源:https://www.cnblogs.com/ghostwu/archive/2018/02/22/8459604.html
-Advertisement-
Play Games

1,魔術方法__set與__get, __call >這些魔術方法,將在相關的屬性或者方法不存在時調用 >函數原型 .function __set( $property, $value ):傳遞屬性的名字和新的值 .function __get( $property ):傳遞屬性的名字,並且返回屬性 ...


1,魔術方法__set與__get, __call

>這些魔術方法,將在相關的屬性或者方法不存在時調用

>函數原型

  .function __set( $property, $value ):傳遞屬性的名字和新的值

  .function __get( $property ):傳遞屬性的名字,並且返回屬性的值

  .function __call( $methods, $args ):傳遞方法的名字和一個數字索引的數組,數組包含傳遞的參數,第一個參數的索引是0

 1 class Coordinate {
 2         private $arr = array( 'x' => null, 'y' => null );
 3         function __get( $property ) {
 4             if( array_key_exists( $property, $this->arr ) ){
 5                 return $this->arr[$property];
 6             }else {
 7                 print "不能訪問一個不存在的鍵名" . PHP_EOL;
 8             }
 9         }
10         function __set( $property, $value ) {
11             if( array_key_exists( $property, $this->arr ) ) {
12                 $this->arr[$property] = $value;
13             }else {
14                 print "不能設置一個不存在的鍵名" . PHP_EOL;
15             }
16         }
17     }
18     
19     $obj = new Coordinate();
20     $obj->x = 10;
21     echo $obj->x . PHP_EOL;
22     $obj->n = 20;
23     echo $obj->n . PHP_EOL;

2,__call的應用

>通過__call可以使HelloWorldDelegator的實例調用HelloWorld的任意存在的方法

 1 class HelloWorld {
 2         function display( $count ){
 3             for( $i = 0 ; $i < $count; $i++ ) {
 4                 echo "Hello World $i " . PHP_EOL;
 5             }
 6             return $count;
 7         }
 8     }
 9 
10     
11     class HelloWorldDelegator {
12         private $obj;
13         function __construct(){
14             $this->obj = new HelloWorld();
15         }
16         function __call( $method, $args ) {
17             return call_user_func_array( array( $this->obj, $method ), $args );
18         }
19     }
20 
21     $obj = new HelloWorldDelegator();
22     print $obj->display( 3 );
23     print PHP_EOL;
24     
25     $obj->show();

3,迭代

>實現一個自己的迭代器,可以通過實現Iterator介面

Iterator介面原型:

Iterator extends Traversable {
/* Methods */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public bool valid ( void )
}
 1 class NumberSquared implements Iterator {
 2     private $start;
 3     private $end;
 4     private $cur;
 5 
 6     function __construct ( $start, $end ){
 7         $this->start = $start;
 8         $this->end = $end;
 9     }
10     function rewind(){ 
11         $this->cur = $this->start;
12     }
13     function key(){
14         return $this->cur;
15     }
16     function current(){
17         return pow( $this->cur, 2 );
18     }
19     function next(){
20         $this->cur++;
21     }
22     function valid(){
23         return $this->cur <= $this->end;
24     }
25 }    
26 
27 $obj = new NumberSquared( 3, 9 );
28 foreach( $obj as $key => $value ){
29     print "the square of $key is $value" . PHP_EOL;
30 }

類本身一般用來表示數據和擁有與這些數據的交互方法,所以一般不需要一個純粹的類迭代器,我們可以實現另一個介面:IteratorAggregate,介面摘要如下:

IteratorAggregate extends Traversable {
/* Methods */
abstract public Traversable getIterator ( void )
}

這個介面只有一個方法,作用是創建一個外部的迭代器介面

改造後的迭代,實現了迭代器與數據處理的分離:

 1 class NumberSquared implements IteratorAggregate {
 2     private $start;
 3     private $end;
 4 
 5     function __construct( $start, $end ) {
 6         $this->start = $start;
 7         $this->end = $end;
 8     }
 9 
10     function getIterator(){
11         return new NumberSquaredIterator( $this );
12     }    
13 
14     function getStart(){
15         return $this->start;
16     }
17 
18     function getEnd(){ 
19         return $this->end;
20     }
21 }
22 
23 class NumberSquaredIterator implements Iterator {
24     private $cur;
25     private $obj;
26     function __construct( $obj ) {
27         $this->obj = $obj;
28     }
29     function rewind(){ 
30         $this->cur = $this->obj->getStart();
31     }
32     function key(){
33         return $this->cur;
34     }
35     function next(){
36         $this->cur++;
37     }
38     function current(){
39         return pow( $this->cur, 2 );
40     }
41     function valid(){
42         return $this->cur <= $this->obj->getEnd();
43     }
44 }
45 
46 $obj = new NumberSquared( 3, 9 );
47 foreach( $obj as $key => $value ) {
48     print "the $key is $value " . PHP_EOL;
49 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String args[]){ System.out.println("file Go..."); // 這裡改成你... ...
  • 如果父類和子類中存在static方法或者變數, 那麼父類對象指向子類引用的時候, 調用的靜態方法或變數都是父類的static方法或變數,與子類引用無關。 因為static修飾的方法或變數不需要使用對象,只用類名就可以調用, 非static的方法需要對象名才能調用,所以父類對象指向子類引用的時候,調用 ...
  • 巴什博奕 巴什博奕: 兩個頂尖聰明的人在玩游戲,有$n$個石子,每人可以隨便拿$1 m$個石子,不能拿的人為敗者,問誰會勝利 巴什博奕是博弈論問題中基礎的問題 它是最簡單的一種情形對應一種狀態的博弈 博弈分析 我們從最簡單的情景開始分析 當石子有$1 m$個時,毫無疑問,先手必勝 當石子有$m+1$ ...
  • 前言 前面介紹了maven相關的SSH開發的,但是在團隊開發中一般都是各自負責各自的,最後再歸結在一起。所以接下來筆者要學習的就是maven的分模塊開發的相關內容。 一、案例需求 基於上邊的三個工程分析 繼承:創建一個parent工程將所需的依賴都配置在pom中 聚合:聚合多個模塊運行 1.1需求描 ...
  • 一、Mybatis 是什麼 ![][1] MyBatis 是一個支持普通SQL查詢、存儲過程和高級映射的優秀持久層框架。MyBatis 消除了幾乎所有的 JDBC 代碼和參數的手工設置以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或註解用於配置和原始映射,將介面和Java的POJO(Pl ...
  • 概述 概述 基於springcloud的單點登錄服務及基於zuul的網關服務(解決了通過zuul轉發到認證服務之後session丟失問題) 基於springcloud的單點登錄服務及基於zuul的網關服務(解決了通過zuul轉發到認證服務之後session丟失問題) 詳細 詳細 代碼下載:http: ...
  • Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11357 Accepted: 3749 Description Georgia and Bob decide to play a self-invented game. They ...
  • ==與equals的主要區別是: ==: ==常用於比較原生類型(基本數據類型):byte,short,char,int,long,float,double,boolean,比較的是他們的值。 若用==來比較兩個對象,則比較的是這兩個對象的記憶體地址。因此,除非是同一個new 出來的對象,比較結果為t ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...