Skip to main content

How to extend the ArrayObject and create a custom array

to my friend with lots of love

class MyArray extends ArrayObject {
  public function __construct($array = array()){ 
    parent::__construct($array, ArrayObject::ARRAY_AS_PROPS);
  }

  public function get_property_string($sep = ':') {
    return implode($sep, $this->getArrayCopy());
  }

  public function __ToString() {
    return 'Array';
  }

  /* some custom method */
}

$rights = new MyArray;
$rights->append('LOGIN');
$rights->append('BETA_ACCESS');
$rights->append('WRITE_COMMENT');

var_dump($rights->get_property_string());

And the output will be:

string(31) "LOGIN:BETA_ACCESS:WRITE_COMMENT

Read more: http://php.net/manual/en/class.arrayobject.php

Related Posts:

  • No Related Posts

Balazs Nadasdi

Developer, Project Manager, Blogger, Dad... or sometihng like these