Content · Web Development · PHP

PHP ResourceBundle::get() Method: Usage and Examples

Looks up a named key or numeric index inside an ICU resource bundle.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

When working with localized application data, ResourceBundle::get() is useful because it looks up a named key or numeric index inside an ICU resource bundle.

Resource bundles store ICU localization data that can vary by locale and installed ICU version.

What Is ResourceBundle::get()?


Looks up a named key or numeric index inside an ICU resource bundle.

Its method signature is:

public function get(string|int $index, bool $fallback = true): mixed

The arguments select the ICU locale and resource bundle, or the key/index to read. A fallback setting controls how ICU handles missing locale-specific entries.

Return value: mixed

Requirements: The PHP intl extension and corresponding ICU resource bundles must be installed.

Example 1: Inspect the basic behavior


Start with a self-contained PHP declaration and inspect the method result.

<?php

declare(strict_types=1);

$resourceBundle = ResourceBundle::create("en", null);
if ($resourceBundle === null) {
    throw new RuntimeException("The ICU resource bundle is unavailable");
}

$result = $resourceBundle->get("Countries");
echo $result instanceof ResourceBundle ? "countries bundle found" : "entry unavailable";

This example illustrates the call and required setup. Execution needs the PHP version or extension noted above.

Explanation: Looks up a named key or numeric index inside an ICU resource bundle.

Example 2: Compare a second case


Next, change the reflected declaration or parameter to observe a related situation.

<?php

declare(strict_types=1);

$resourceBundle = ResourceBundle::create("fr", null);
if ($resourceBundle === null) {
    throw new RuntimeException("The ICU resource bundle is unavailable");
}

$result = $resourceBundle->get("Countries");
echo $result instanceof ResourceBundle ? "countries bundle found" : "entry unavailable";

// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);

This example illustrates the call and required setup. Execution needs the PHP version or extension noted above.

Explanation: An unknown key may return null or false and set an intl error; bundle structure depends on ICU data.

Practical Example: Apply the operation in context


This final case shows how the same API can be used when inspecting application definitions.

<?php

declare(strict_types=1);

$resourceBundle = ResourceBundle::create("de", null);
if ($resourceBundle === null) {
    throw new RuntimeException("The ICU resource bundle is unavailable");
}

$result = $resourceBundle->get("Countries");
echo $result instanceof ResourceBundle ? "countries bundle found" : "entry unavailable";

// Use the reflection result in a simple decision.
if (is_bool($result)) {
    echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
    echo "\nEntries: " . count($result);
} elseif ($result === null) {
    echo "\nNo value returned";
} else {
    echo "\nReflection result available";
}

This example illustrates the call and required setup. Execution needs the PHP version or extension noted above.

Explanation: create opens the resource bundle before accessing entries.

Important Behavior and Edge Cases


  • An unknown key may return null or false and set an intl error; bundle structure depends on ICU data.
  • create opens the resource bundle before accessing entries.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ResourceBundle::get()Looks up a named key or numeric index inside an ICU resource bundle.
Related APIcreate opens the resource bundle before accessing entries.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: An unknown key may return null or false and set an intl error; bundle structure depends on ICU data.Handle the specific limitation: An unknown key may return null or false and set an intl error; bundle structure depends on ICU data.
Confusing this method with Related APIcreate opens the resource bundle before accessing entries.

Key Takeaways


  • Looks up a named key or numeric index inside an ICU resource bundle.
  • An unknown key may return null or false and set an intl error; bundle structure depends on ICU data.
  • create opens the resource bundle before accessing entries.

Use ResourceBundle::get() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.

Share this resourceSend this page to someone who may find it useful.