published on in HowTo
tags: php

How to extend the ArrayObject and create a custom array

To my friend with lots of love

<?php
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