忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/04/26 20:30 |
PHP1-67:オブジェクトに配列でアクセスするまたまた続き

前回のをさらに改良。
intArrayをInteger以外にも対応するように変更しました。

classArray.class.php 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
    
class classArray implements ArrayAccess,IteratorAggregate,Countable{
    
    //適用可能な型
    private $type_valid=array(
         'string'
        ,'integer'
        ,'float'
        ,'boolean'
    );
    
    //
    private $type='';
    
    //配列
    private $arr=array();
    
    //コンストラクタ
    function __construct($type_valid='integer'){
        //引数が適用可能な型でなければException
        if(!in_array($type_valid,$this->type_valid)){
            $debug=debug_backtrace();
            throw new InvalidArgumentException(
                'Value is not valid type in line &lt;'.$debug[0]['line'].'&gt;'
            );
            return false;
        }
        $this->type=$type_valid;
        return true;
    }
    
    //マジックメソッド__set
    public function __set($a,$b){
        return $this->offsetSet($a,$b);
    }
    //マジックメソッド__get
    public function __get($a){
        return $this->offsetGet($a);
    }
    
    //引数の型チェック
    private function _chechOffsetType($b){
        switch($this->type){
        case 'string':
            return is_string($b);break;
        case 'integer':
            return is_int($b);break;
        case 'float':
            return is_float($b);break;
        case 'boolean':
            return is_bool($b);break;
        }
        return false;
    }
    
    //値セット        ArrayAccess::offsetSet
    public function offsetSet($a,$b){
        //引数が正しい型かチェック
        if(!$this->_chechOffsetType($b)){
            //正しい型でなければException
            $debug=debug_backtrace();
            throw new InvalidArgumentException(
                'Value is not '.$this->type.' in line &lt;'.$debug[0]['line'].'&gt;'
                .' type &lt;'.gettype($b).'&gt;'
            );
            return false;
        }
        
        //挿入
        if($a===NULL){
            $this->arr[]=$b;
        }else{
            $this->arr[$a]=$b;
        }
        return true;
    }
    
    //値が存在するか        ArrayAccess::offsetExists
    public function offsetExists($a){
        if($a===NULL){return false;}
        return isset($this->arr[$a]);
    }
    
    //値を取得        ArrayAccess::offsetGet
    public function offsetGet($a){
        if($this->offsetExists($a)){
            return $this->arr[$a];
        }else{
            return null;
        }
    }
    
    //値削除        ArrayAccess::offsetUnset
    public function offsetUnset($a){
        if($this->offsetExists($a)){
            unset($this->arr[$a]);
        }
    }
    
    //個数カウント        Countable::count
    public function count(){
        return count($this->arr);
    }
    
    //イテレータ        IteratorAggregate::getIterator
    public function getIterator(){
        return new ArrayIterator($this->arr);
    }
 
#↓クラスのおわり
}


new時に'boolean','integer','double','string'何れかの引数を指定できます。
そしてその後、それ以外の型の値を代入するとエラーになります。
判定は厳密に行われるので、string型にintegerを放り込むこともできません。

前回から変更したのはコンストラクタとoffsetSetです。
コンストラクタでは引数として型を指定し、offsetSetではコンストラクタの引数として指定した型で型チェックを行います。
型チェック部分は最初evalとかで簡単にできないかなーと思ったんですが、いまいちうまく行かないのでやめました。
evalは怖いですし。

classArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
    
    //インスタンス
        require_once('classArray.class.php');
        $str=new classArray('string');
        
    //代入
        try{
            $str[]='1';
            $str[]=1;        //ここでInvalidArgumentException
        }catch(InvalidArgumentException $e){
            print($e->getMessage());
        }
        
        print("<pre>");var_dump($str);


インスタンス生成時に'string'や'double'等と書くと、その型しか格納できない配列が作成できます。
値として真偽値のみが許される配列を作りたい、といった場合に使えるかも使えないかも。

 

PR


2009/04/13 17:14 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<PHP1-68:オブジェクトに配列でアクセスするしつこく続き | HOME | PHP1-66:オブジェクトに配列でアクセスするさらに続き>>
忍者ブログ[PR]