Page MenuHomePhorge
Diviner Arcanist Tech Docs PhutilArrayWithDefaultValue

final class PhutilArrayWithDefaultValue
Arcanist Technical Documentation ()

Array-like object with a default value that is populated when nonexistent keys are accessed. This is particularly useful to avoid constructs like this:

$counts = array();
foreach ($fruit as $fruit) {
  if (!isset($counts[$fruit])) {
    $counts[$fruit] = 0;
  }
  $counts[$fruit] += 1;
}

Instead, you can use PhutilArrayWithDefaultValue:

$counts = new PhutilArrayWithDefaultValue();
foreach ($fruits as $fruit) {
  $counts[$fruit] += 1;
}
NOTE: You must use the += 1 operator, not ++, because PHP is the crown jewel of programming languages.

Normally, the default value is 0 to allow the object to be used to count things or build bitmasks. You can change the default with setDefaultValue().

Methods

public function __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

public function current()
Inherited

This method is not documented.
Return
wild

public function key()
Inherited

This method is not documented.
Return
wild

public function next()
Inherited

This method is not documented.
Return
wild

public function rewind()
Inherited

This method is not documented.
Return
wild

public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

public function __construct($initial_value)
Inherited

This method is not documented.
Parameters
array$initial_value
Return
this//Implicit.//

public function toArray()
Inherited

This method is not documented.
Return
wild

public function count()
Inherited

This method is not documented.
Return
wild

public function offsetExists($key)

This method is not documented.
Parameters
$key
Return
wild

public function offsetGet($key)

This method is not documented.
Parameters
$key
Return
wild

public function offsetSet($key, $value)
Inherited

This method is not documented.
Parameters
$key
$value
Return
wild

public function offsetUnset($key)
Inherited

This method is not documented.
Parameters
$key
Return
wild

public function setDefaultValue($default_value)

This method is not documented.
Parameters
$default_value
Return
wild