File
in package
Class for handling JSON files with object-oriented methods and properties.
Table of Contents
Properties
- $data : array<string|int, mixed>|null
- JSON file data
- $filepath : string
- Path to the JSON file
- $lastError : string|null
- Last error encountered
- $loaded : bool
- File loading status
Methods
- __construct() : mixed
- JsonFile class constructor
- exists() : bool
- Checks if the file exists
- get() : mixed
- Gets a specific value using dot notation
- getData() : array<string|int, mixed>|null
- Gets all JSON data
- getFileInfo() : array<string|int, mixed>|null
- Gets file information
- getFilepath() : string
- Gets the file path
- getLastError() : string|null
- Gets the last error that occurred
- has() : bool
- Checks if a specific key exists
- isLoaded() : bool
- Checks if the file is loaded
- load() : bool
- Loads the JSON file content
- remove() : bool
- Removes a specific key
- save() : bool
- Saves current data to the JSON file
- set() : void
- Sets a specific value using dot notation
- setData() : void
- Sets the JSON data
- setFilepath() : void
- Changes the file path
Properties
$data
JSON file data
private
array<string|int, mixed>|null
$data
$filepath
Path to the JSON file
private
string
$filepath
$lastError
Last error encountered
private
string|null
$lastError
$loaded
File loading status
private
bool
$loaded
Methods
__construct()
JsonFile class constructor
public
__construct(string $filepath) : mixed
Parameters
- $filepath : string
-
Path to the JSON file
exists()
Checks if the file exists
public
exists() : bool
Return values
bool —true if exists, false otherwise
get()
Gets a specific value using dot notation
public
get(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Key in dot notation (e.g., "user.name")
- $default : mixed = null
-
Default value if key is not found
Return values
mixed —The found value or default value
getData()
Gets all JSON data
public
getData() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —Array with data or null if not loaded
getFileInfo()
Gets file information
public
getFileInfo() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —Array with file information or null if it doesn't exist
getFilepath()
Gets the file path
public
getFilepath() : string
Return values
string —File path
getLastError()
Gets the last error that occurred
public
getLastError() : string|null
Return values
string|null —Last error message or null if no errors
has()
Checks if a specific key exists
public
has(string $key) : bool
Parameters
- $key : string
-
Key in dot notation
Return values
bool —true if exists, false otherwise
isLoaded()
Checks if the file is loaded
public
isLoaded() : bool
Return values
bool —true if loaded, false otherwise
load()
Loads the JSON file content
public
load() : bool
Return values
bool —true if loaded successfully, false otherwise
remove()
Removes a specific key
public
remove(string $key) : bool
Parameters
- $key : string
-
Key in dot notation
Return values
bool —true if removed, false if it didn't exist
save()
Saves current data to the JSON file
public
save([bool $prettyPrint = true ]) : bool
Parameters
- $prettyPrint : bool = true
-
Whether to format JSON with indentation
Return values
bool —true if saved successfully, false otherwise
set()
Sets a specific value using dot notation
public
set(string $key, mixed $value) : void
Parameters
- $key : string
-
Key in dot notation (e.g., "user.name")
- $value : mixed
-
Value to set
setData()
Sets the JSON data
public
setData(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
-
Data to set
setFilepath()
Changes the file path
public
setFilepath(string $filepath) : void
Parameters
- $filepath : string
-
New file path