zoukankan      html  css  js  c++  java
  • PHP几个接品 ArrayAccess, Iterator Countable 简单

    echo "PHP几个接品 ArrayAccess, Iterator Countable<BR><BR>";
    echo "<h1>Countable</h1>";
    /*abstract public int count(void)
    {
    }*/
    class MyCounter implements Countable
    {
    	function count()
    	{
    		static $count = 0;
    		return ++$count;
    	}
    	public function count2()
    	{
    		static $count2 = 0;
    		return ++$count2;
    	}
    }
    
    $count = new MyCounter();
    for($i=0; $i<6; $i++)
    {
    	echo "\n this counter is :".count($count)."<BR>";
    	//echo "\n this counter is :".count($count2)."<BR>";
    }
    //实现对类的count,但实际的用处并不是很清楚,其实意思也不太明白了,
    //莫法上网莫法查啊
    echo "<h1>ArrayAccess</h1>";
    /*ArrayAccess
    {
    	abstract public boolean offsetExists (string $offset);
    	abstract public mixed offsetGet(string $offset);
    	abstract public void offsetSet(string $offset, string $value);
    	abstract public void offsetUnset(string $offset);
    }*/
    class obj implements ArrayAccess
    {
    	private $contain = array();
    
    	function __construct()
    	{
    		$this->contain = array('gao'=>'gao','song'=>'song');
    	}
    
    	
    	function __destruct()
    	{
    		unset($this->contain);
    	}
    
    	public function offsetExists($offset)
    	{
    		return isset($this->contain[$offset]) ? TRUE:FALSE;
    	}
    
    	public function offsetGet($offset)
    	{
            return isset($this->contain[$offset]) ? $this->contain[$offset] : null;
    	}
    
    	public function offsetSet($offset,$value)
    	{
            $this->contain[$offset] = $value;
    	}
    
    	public function offsetUnset($offset)
    	{
    		unset($this->contain[$offset]);
    	}
    }
    
    $obj = new obj();
    $obj['name'] = 'xlc';
    $obj['sex'] = '男';
    echo $obj['name']." ".$obj['sex'];
    echo "<BR><BR>";
    //实现对类的取值赋值等操作
    
    echo "<h1>Iterator</h1>";
    //Traversable{}
    /*Iterator extends Traversable
    {
    	abstract public mixed current(void);
    	abstract public scalar key(void);
    	abstract public void next(void);
    	abstract public void rewind(void);
    	abstract public boolean valid(void);
    }*/
    class MyIterator implements Iterator
    {
    	private $index = 0;
    	private $configtion = array('one','two','three');
    
    	function __construct()
    	{
    		$this->index = 0;
    	}
    
    	function __destruct()
    	{
    		unset($this->configtion);
    	}
    
    	public function current()
    	{
    		return $this->configtion[$this->index];
    	}
    
    	public function key()
    	{
    		return $this->index;
    	}
    
    	public function next()
    	{
    		return ++$this->index;
    	}
    
    	public function rewind()
    	{
    		$this->index = 0;
    	}
    
    	public function valid()
    	{
    		return isset($this->configtion[$this->index]) ? TRUE : FALSE;
    	}
    }
    
    $it = new MyIterator();
    foreach($it as $key=>$val)
    {
        echo "\n======{$key}===={$val}<BR>";
    }
    $it->rewind();
    echo $it->key();
    echo $it->current();
    

      

  • 相关阅读:
    php数据过滤函数与方法示例【转载】
    MySQL 数据类型 详解 (转载)
    【总结整理】产品经理-电台
    【总结整理】职业选择
    【总结整理】什么样的产品是好产品
    【总结整理】微信产品-张小龙-PM学习总结
    Arcgis engine编程报错查询(转)
    ArcEngine开发遇到的问题(转)
    【总结整理】OpenLayers项目分析,OpenLayers中的图层,GeoServer发布wms服务--实验(转)
    带你剖析WebGis的世界奥秘----Geojson数据加载(高级)(转)
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/3055642.html
Copyright © 2011-2022 走看看