URL
URL
class URL implements \ArrayAccess , \JsonSerializableImplements: ArrayAccess, JsonSerializable
Properties
commonPorts
public array $commonPorts = array (
'ftp' => 21,
'http' => 80,
'https' => 443,
)Type: array
params
protected array $paramsType: array
parts
protected array $parts = array (
'scheme' => 'http',
'host' => 'localhost',
'path' => '/',
)Type: array
Methods
__construct
Constructs a new URL object, optionally parsing a URL string.
public __construct(?string $url): voidIf a URL string is provided, it will be parsed into its components. If the string does not contain a scheme, 'http://' will be prepended. Query parameters are extracted and stored separately.
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL string to parse, or null for a default URL |
__get
public __get(string $key): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string |
__set
public __set(string $key, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | |
$value | mixed |
__toString
Returns the URL as a string.
public __toString(): stringReturns: string
This magic method allows the URL object to be used in a string context.
build
Creates a new instance of the URL class using the provided URL string.
public build(string $url): selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL string to initialize the instance with. Defaults to an empty string. |
scheme
Gets or sets the scheme part of the URL.
public scheme(?string $value): stringReturns: string
If called with no arguments, returns the current scheme (default: 'http'). If called with one argument, sets the scheme to the provided value.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string | The scheme to set, or null to get the current scheme |
host
Gets or sets the host part of the URL.
public host(?string $value): stringReturns: string
If called with no arguments, returns the current host. If called with one argument, sets the host to the provided value.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string | The host to set, or null to get the current host |
port
Gets or sets the port part of the URL.
public port(?int $value): intReturns: int
If called with no arguments, returns the current port. If the port is not set, it attempts to determine the default port for the scheme. If called with an argument, sets the port to the provided value.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | int | The port to set, or null to get the current port |
lookupPort
Looks up the default port number for a given URL scheme.
public lookupPort(string $scheme): ?intReturns: int
First checks the commonPorts array for well-known schemes. If not found, attempts to parse the /etc/services file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$scheme | string | The URL scheme (e.g., 'http', 'https', 'ftp') |
user
Gets or sets the user part of the URL.
public user(): stringReturns: string
If called with no arguments, returns the current user. If called with one argument, sets the user to the provided value.
pass
Gets or sets the password part of the URL.
public pass(): stringReturns: string
If called with no arguments, returns the current password. If called with one argument, sets the password to the provided value.
path
Gets or sets the path part of the URL.
public path(): stringReturns: string
If called with no arguments, returns the current path, always starting with '/'. If called with one argument, sets the path to the provided value.
params
Gets or sets the query parameters of the URL.
public params(): arrayReturns: array
If called with no arguments, returns the current parameters as an associative array. If called with one argument (an array), replaces the parameters with the provided array and returns it.
hash
Gets or sets the fragment (hash) part of the URL.
public hash(): stringReturns: string
If called with no arguments, returns the current fragment (without the leading '#'). If called with one argument, sets the fragment to the provided value.
get
Retrieves a parameter value by key.
public get(string $key): int|stringReturns: int|string
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The parameter key to retrieve |
set
Sets a parameter value by key.
public set(string $key, int|string $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The parameter key to set |
$value | int | string |
setParams
Sets the URL parameters.
public setParams(array $array, bool $merge = true): voidReturns: void
If $merge is true, merges the provided array with the existing parameters. If $merge is false, replaces the parameters with the provided array.
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | |
$merge | bool | whether to merge with existing parameters (default: true) |
offsetExists
Checks if a parameter exists in the URL parameters array.
public offsetExists(mixed $offset): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | The parameter key to check |
offsetGet
Retrieves a parameter value by key using array access.
public offsetGet(mixed $offset): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | The parameter key to retrieve |
offsetSet
Sets a parameter value by key using array access.
public offsetSet(mixed $key, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | The parameter key to set |
$value | mixed | The value to assign to the parameter |
offsetUnset
Unsets a parameter from the URL parameters array.
public offsetUnset(mixed $key): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | the parameter key to unset |
isSecure
Determines if the URL uses a secure scheme (e.g., HTTPS).
public isSecure(): boolReturns: bool
toString
Returns the URL as a string, optionally substituting parameter placeholders with provided values.
public toString(?array $values): stringReturns: string
If $values is provided, any parameter value in the form of "$key" will be replaced by the corresponding value from $values.
Parameters
| Parameter | Type | Description |
|---|---|---|
$values | array |
getOrigin
Retrieves the origin of the URL.
public getOrigin(): stringReturns: string
This method extracts the origin from the rendered URL by using a regular expression pattern. The origin is defined as the protocol and domain of the URL.
queryString
Returns the query string for the current URL parameters.
public queryString(): ?stringReturns: string
If there are parameters present, returns them as a URL-encoded query string prefixed with '?'. If there are no parameters, returns null.
appendPath
Appends a path segment to the current URL path.
public appendPath(string $path): voidReturns: void
This method ensures that there are no duplicate slashes between the existing path and the appended segment.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the path segment to append |
jsonSerialize
Returns the URL as a JSON serialized string.
public jsonSerialize(): mixedReturns: mixed
buildQuery
Builds a query string from the provided parameters.
protected buildQuery(array $params): stringReturns: string
Parameters
| Parameter | Type | Description |
|---|---|---|
$params | array |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:18 +0000