时间:2021-07-01 10:21:17 帮助过:32人阅读
ArrayAccess是一个interface,实现这个interface,必须要实现以下几个方法:
interfaceArrayAccess
{//标示一个元素是否定义publicfunctionoffsetExists($offset);//返回一个元素值publicfunctionoffsetGet($offset);//为一个元素赋值publicfunctionoffsetSet($offset,$value);//删除一个元素值publicfunctionoffsetUnset($offset);
}
实现:
classTestimplementsArrayAccess
{private$elements = array();
//标示一个元素是否定义publicfunctionoffsetExists($offset)
{returnisset($this->elements[$offset]);
}
//返回一个元素值publicfunctionoffsetGet($offset)
{return$this->elements[$offset];
}
//为一个元素赋值publicfunctionoffsetSet($offset,$value)
{$this->elements[$offset] = $value;
}
//删除一个元素值publicfunctionoffsetUnset($offset)
{unset($this->elements[$offset]);
}
}
$test = new Test();
$test['test'] = 'test';//自动调用offsetSetif(isset($test['test']))//自动调用offsetExists
{
echo$test['test'];//自动调用offsetGetecho'
';
unset($test['test']);//自动调用offsetUnset var_dump($test['test']);
}
classobjimplementsArrayAccess
{private$container = array();
publicfunction__construct()
{$this->container = array(
'one' => 1,
'tow' => 2,
'three' => 3,
);
}
publicfunctionoffsetGet($offset)
{returnisset($this->container[$offset]) ? $this->container[$offset] : null;
}
publicfunctionoffsetSet($offset,$value)
{if (is_null($offset)){
$this->container = $value;
}else{
$this->container[$offset] = $value;
}
}
publicfunctionoffsetExists($offset)
{returnisset($this->container[$offset]);
}
publicfunctionoffsetUnset($offset)
{unset($this->container[$offset]);
}
}
$obj = new obj;
var_dump(isset($obj["two"])); // 调用 offsetExists
var_dump($obj["two"]); //调用 offsetGetunset($obj["two"]); //调用 offsetUnset
var_dump(isset($obj["two"]));// 调用 offsetExists$obj["two"] = "A value"; // 调用 offsetSet
var_dump($obj["two"]); //调用 offsetGet$obj[] = 'Append 1';
$obj[] = 'Append 2';
$obj[] = 'Append 3';
print_r($obj);
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了1ArrayAccess(数组式访问)接口,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。