When working with localized application data, ResourceBundle::create() is useful because it opens an ICU resource bundle for a locale and bundle identifier.
Resource bundles store ICU localization data that can vary by locale and installed ICU version.
What Is ResourceBundle::create()?
Opens an ICU resource bundle for a locale and bundle identifier.
Its method signature is:
public static function create(?string $locale, ?string $bundle, bool $fallback = true): ?ResourceBundle
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: ?ResourceBundle
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);
$result = ResourceBundle::create("en", null);
echo $result instanceof ResourceBundle ? "bundle opened" : "bundle unavailable";
This example illustrates the call and required setup. Execution needs the PHP version or extension noted above.
Explanation: Opens an ICU resource bundle for a locale and bundle identifier.
Example 2: Compare a second case
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
$result = ResourceBundle::create("fr", null);
echo $result instanceof ResourceBundle ? "bundle opened" : "bundle 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: Requires the intl extension and suitable ICU resource data; missing bundles can return null.
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);
$result = ResourceBundle::create("de", null);
echo $result instanceof ResourceBundle ? "bundle opened" : "bundle 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: ResourceBundle::get accesses an entry from an opened bundle.
Important Behavior and Edge Cases
- Requires the intl extension and suitable ICU resource data; missing bundles can return null.
- ResourceBundle::get accesses an entry from an opened bundle.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ResourceBundle::create() | Opens an ICU resource bundle for a locale and bundle identifier. |
Related API | ResourceBundle::get accesses an entry from an opened bundle. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Requires the intl extension and suitable ICU resource data; missing bundles can return null. | Handle the specific limitation: Requires the intl extension and suitable ICU resource data; missing bundles can return null. |
| Confusing this method with Related API | ResourceBundle::get accesses an entry from an opened bundle. |
Key Takeaways
- Opens an ICU resource bundle for a locale and bundle identifier.
- Requires the intl extension and suitable ICU resource data; missing bundles can return null.
- ResourceBundle::get accesses an entry from an opened bundle.
Use ResourceBundle::create() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.