Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2894819
PhutilArrayWithDefaultValue.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Advanced/Developer...
View Handle
View Hovercard
Size
1 KB
Referenced Files
None
Subscribers
None
PhutilArrayWithDefaultValue.php
View Options
<?php
/**
* 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
* @{method:setDefaultValue}.
*/
final
class
PhutilArrayWithDefaultValue
extends
PhutilArray
{
private
$defaultValue
=
0
;
public
function
setDefaultValue
(
$default_value
)
{
$this
->
defaultValue
=
$default_value
;
return
$this
;
}
public
function
offsetExists
(
$key
)
{
return
true
;
}
public
function
offsetGet
(
$key
)
{
if
(
!
parent
::
offsetExists
(
$key
)
)
{
$this
->
offsetSet
(
$key
,
$this
->
defaultValue
)
;
}
return
parent
::
offsetGet
(
$key
)
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sun, Jan 19, 20:32 (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1115342
Default Alt Text
PhutilArrayWithDefaultValue.php (1 KB)
Attached To
Mode
rARC Arcanist
Attached
Detach File
Event Timeline
Log In to Comment