php8.5
Home/ Manual/ spl / arrayobject/ ArrayObject::setFlags

ArrayObject::setFlags

PHP function Edit on GitHub ✎

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Sets the behavior flags

Description

ArrayObject::setFlags(int $flags): void

Set the flags that change the behavior of the ArrayObject.

Parameters

flags

The new ArrayObject behavior. It takes on either a bitmask, or named constants. Using named constants is strongly encouraged to ensure compatibility for future versions.

The available behavior flags are listed below. The actual meanings of these flags are described in the predefined constants.

valueconstant
1ArrayObject::STD_PROP_LIST
2ArrayObject::ARRAY_AS_PROPS

Return Values

Void

Examples

ArrayObject::setFlags() example

php
<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);

$fruitsArrayObject = new ArrayObject($fruits);

// Try to use array key as property
var_dump($fruitsArrayObject->lemons);
// Set the flag so that the array keys can be used as properties of the ArrayObject
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);
// Try it again
var_dump($fruitsArrayObject->lemons);
?>

The above example will output something similar to:

output
Warning: Undefined property: ArrayObject::$lemons in ...
NULL
int(1)

Source: reference/spl/arrayobject/setflags.xml · from the official PHP manual (php/doc-en)