The PHP Error::getMessage API performs a focused operation on an object or value. The examples below show its effect on actual data.
Returns the descriptive message attached to a Throwable.
What Is Error::getMessage in PHP?
Returns the descriptive message attached to a Throwable.
Syntax:
Error::getMessage(): string
This operation has no parameters.
Return type declared by PHP: string. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$originalError = new Error("Unable to process order", 17, new RuntimeException("Underlying failure"));
echo $originalError->getMessage();
Output:
Unable to process order
Returns the descriptive message attached to a Throwable.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$originalError = new Error("Unable to read configuration", 42, new RuntimeException("Missing file"));
echo $originalError->getMessage();
Output:
Unable to read configuration
The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$originalError = new Error("Unexpected response", 503, new RuntimeException("Timeout"));
echo $originalError->getMessage();
Output:
Unexpected response
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors.
- Returns the descriptive message attached to a Throwable.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Error::getMessage | Returns the descriptive message attached to a Throwable. |
| Exception | Error generally represents engine-level failures while Exception is intended for recoverable application conditions; both implement Throwable. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Error::getMessage | The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Error generally represents engine-level failures while Exception is intended for recoverable application conditions; both implement Throwable. |
Key Takeaways
- Returns the descriptive message attached to a Throwable.
- The line number and trace describe where the exception object was created or thrown; do not reveal full traces to public visitors.
- Use the example that matches the values, runtime version and dependencies in your application.
Error::getMessage is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.