PHAR archives can package multiple resources into a single file. Phar::getModified() Method addresses a specific part of that process: reports whether a Phar archive has been changed since it was opened. The examples distinguish archive-level effects from member-level effects.
What Is Phar::getModified() Method?
Reports whether a Phar archive has been changed since it was opened.
Its syntax is:
Phar::getModified()
This function or method takes no parameters.
Return value: The declared return type is bool. This boolean concerns archive modifications, not filesystem last-modified time.
Requirements: The Phar extension must be enabled. Creating or modifying executable PHAR archives generally requires the phar.readonly setting to be disabled before startup; do not change server configuration just to view this guide.
Important: This boolean concerns archive modifications, not filesystem last-modified time.
Example 1: Basic Usage
The basic example focuses on the behavior of Phar::getModified() Method: reports whether a Phar archive has been changed since it was opened. Inputs are defined locally or shown as application-controlled paths.
<?php
$archivePath = sys_get_temp_dir() . "/example-package.phar";
$archive = new Phar($archivePath);
$archive->addFromString("new.txt", "updated");
var_dump($archive->getModified());
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for Phar::getModified() Method. Notice the different checks, arguments, or output handling.
<?php
$archive = new Phar(sys_get_temp_dir() . "/dirty.phar");
$archive->startBuffering();
$archive->addFromString("version.txt", "2");
var_dump($archive->getModified());
$archive->stopBuffering();
This variation matters because this boolean concerns archive modifications, not filesystem last-modified time.
Practical Example: Working with Archive Entries
The following example demonstrates how Phar::getModified() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$archive = new Phar(sys_get_temp_dir() . "/change-tracking.phar");
$archive->startBuffering();
$archive->addFromString("version.txt", "3");
if ($archive->getModified()) { echo "Archive changed"; }
$archive->stopBuffering();
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Reports whether a Phar archive has been changed since it was opened.
- Limitation: This boolean concerns archive modifications, not filesystem last-modified time.
- Availability: The Phar extension must be enabled. Creating or modifying executable PHAR archives generally requires the phar.readonly setting to be disabled before startup; do not change server configuration just to view this guide.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
Phar::getModified() Method | Reports whether a Phar archive has been changed since it was opened. |
Phar::getMetadata() | Reads stored archive metadata; getModified() instead indicates changes since archive opening. |
Use Phar::getModified() Method for the specific behavior described above. The related operation, Phar::getMetadata(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing archive-level operations with operations on individual entries. | This boolean concerns archive modifications, not filesystem last-modified time. Confirm the expected archive format, member path and read/write permissions. |
| Treating Phar::getMetadata() as an interchangeable replacement for Phar::getModified() Method. | Reads stored archive metadata; getModified() instead indicates changes since archive opening. |
Key Takeaways
- Reports whether a Phar archive has been changed since it was opened.
- This boolean concerns archive modifications, not filesystem last-modified time.
- Related operation: Phar::getMetadata() — Reads stored archive metadata; getModified() instead indicates changes since archive opening.
Phar::getModified() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.