Home > Backend Development > PHP Tutorial > PHP overloaded array operator_PHP tutorial

PHP overloaded array operator_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-20 11:17:57
Original
1827 people have browsed it

PHP provides many interfaces to implement some very specific functions. For example, if you want to use an object as an array, you only need to implement the ArrayAccess interface. When you want to use an object in foreach When , you only need to implement the Iterator interface. Here is an example

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

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

class BtstoreRoot

{

 

    /**

     * 根结点

     * @var BtstoreElement

     */

    static $root;

}

 

class BtstoreElement implements ArrayAccess, Iterator

{

 

    /**

     * 当前所代表的目录

     * @var string

     */

    private $dataDir;

 

    /**

     * 当前所代表的数据

     * @var array

     */

    private $arrData;

 

    /**

     * 构造函数

     * @param string $dataDir

     * @param array $arrData

     */

    function __construct($dataDir, $arrData)

    {

 

        $this->dataDir = '';

        $this->arrData = array ();

        if (! empty ( $dataDir ) && is_dir ( $dataDir ))

        {

            $this->dataDir = $dataDir;

        }

 

        if (! empty ( $arrData ))

        {

            $this->arrData = $arrData;

        }

    }

 

    function __get($key)

    {

 

        if (isset ( $this->arrData [$key] ))

        {

            $data = $this->arrData [$key];

            if (is_array ( $data ) && ! is_object ( $data ))

            {

                $data = new BtstoreElement ( '', $data );

            }

            return $data;

        }

 

        if (! empty ( $this->dataDir ))

        {

            $path = $this->dataDir . '/' . $key;

            if (is_dir ( $path ))

            {

                $data = new BtstoreElement ( $path, null );

                $this->arrData [$key] = $data;

                return $data;

            }

 

            if (is_file ( $path ))

            {

                $content = file_get_contents ( $path );

                $arrData = unserialize ( $content );

                $data = new BtstoreElement ( '', $arrData );

                $this->arrData [$key] = $data;

                return $data;

            }

        }

 

        trigger_error ( "undefined index:$key" );

    }

 

    function __isset($key)

    {

 

        if (isset ( $this->arrData [$key] ))

        {

            return true;

        }

 

        if (file_exists ( $this->dataDir . '/' . $key ))

        {

            return true;

        }

 

        return false;

    }

 

    function toArray()

    {

 

        return $this->arrData;

    }

 

    /* (non-PHPdoc)

     * @see ArrayAccess::offsetExists()

     */

    public function offsetExists($offset)

    {

 

        return $this->__isset ( $offset );

    }

 

    /* (non-PHPdoc)

     * @see ArrayAccess::offsetGet()

     */

    public function offsetGet($offset)

    {

 

        return $this->__get ( $offset );

    }

 

    /* (non-PHPdoc)

     * @see ArrayAccess::offsetSet()

     */

    public function offsetSet($offset, $value)

    {

 

        trigger_error ( 'offsetSet not implemented by BtstoreElement' );

    }

 

    /* (non-PHPdoc)

     * @see ArrayAccess::offsetUnset()

     */

    public function offsetUnset($offset)

    {

 

        trigger_error ( 'offsetUnset not implemented by BtstoreElement' );

    }

 

    /* (non-PHPdoc)

     * @see Iterator::current()

     */

    public function current()

    {

 

        return current ( $this->arrData );

    }

 

    /* (non-PHPdoc)

     * @see Iterator::next()

     */

    public function next()

    {

 

        return next ( $this->arrData );

    }

 

    /* (non-PHPdoc)

     * @see Iterator::key()

     */

    public function key()

    {

 

        return key ( $this->arrData );

    }

 

    /* (non-PHPdoc)

     * @see Iterator::valid()

     */

    public function valid()

    {

 

        $data = current ( $this->arrData );

        return ! empty ( $data );

    }

 

    /* (non-PHPdoc)

     * @see Iterator::rewind()

     */

    public function rewind()

    {

 

        reset ( $this->arrData );

    }

 

}

 

/**

 * 获取一个BtstoreElement对象

 * @return BtstoreElement

 */

function btstore_get()

{

 

    if (empty ( BtstoreRoot::$root ))

    {

        BtstoreRoot::$root = new BtstoreElement ( ScriptConf::BTSTORE_ROOT, null );

    }

 

    return BtstoreRoot::$root;

}

Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371907.htmlTechArticle provides many interfaces in php to implement some very specific functions. For example, if you want to use an object as When using array, you only need to implement the ArrayAccess interface. When you want foreach to be able to...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template