Array classes in PHP -
i'm trying make array class , link php file follows:
<?php $myarray=array('1','2','3'); array1_class=new array_class($myarray);
and want try create class can put array in , call them follows:
array1_class->data[1]; // should display '2' example
is there way can this?
<?php class mylamearray { /** @var array $data */ public $data; /** * @param array $data */ public function __construct(array $data) { $this->data = $data; } /** * @param string $key * @param mixed $value * @return bool */ public function set($key, $value) { $this->data[$key] = $value; } /** * @param string $key * @return mixed|null */ public function get($key) { return isset($this->data[$key]) ? $this->data[$key] : null; } /** * @param string $key * @return bool */ public function has($key) { return isset($this->data[$key]); } }
which can use this
$data = array( 'food' => 'fried chicken', ); $x = new mylamearray($data); echo $x->has('fail') ? 'fail var set' : 'no fail var set'; echo "\n"; echo $x->has('food') ? 'food var set' : 'no food var set'; echo 'food set '.$x->get('food'); echo "\n"; $x->set('food', 'burger'); echo $x->data['food'];
see here: https://3v4l.org/p9cdd
Comments
Post a Comment