URL
URL
Properties
commonPorts
public array $commonPorts = array (
'ftp' => 21,
'http' => 80,
'https' => 443,
)params
protected array $paramsparts
protected array $parts = array (
'scheme' => 'http',
'host' => 'localhost',
'path' => '/',
)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): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$key | string |
__set
public __set(string $key, mixed $value): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | |
$value | mixed |
__toString
Returns the URL as a string.
public __toString(): stringThis 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): selfParameters
| 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): stringIf 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): stringIf 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): intIf 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): ?intFirst 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(): stringIf 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(): stringIf 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(): stringIf 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(): voidIf 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(): stringIf 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): stringParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The parameter key to retrieve |
set
Sets a parameter value by key.
public set(string $key, string $value): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The parameter key to set |
$value | string | The value to assign to the parameter |
setParams
Sets the URL parameters.
public setParams(array $array, bool $merge = true): voidIf $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): boolParameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | The parameter key to check |
offsetGet
Retrieves a parameter value by key using array access.
public offsetGet(mixed $offset): mixedParameters
| 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): voidParameters
| 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): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | the parameter key to unset |
isSecure
Determines if the URL uses a secure scheme (e.g., HTTPS).
public isSecure(): booltoString
Returns the URL as a string, optionally substituting parameter placeholders with provided values.
public toString(?array $values): stringIf $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(): stringThis 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(): ?stringIf 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): voidThis 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(): mixedbuildQuery
Builds a query string from the provided parameters.
protected buildQuery(array $params): stringParameters
| Parameter | Type | Description |
|---|---|---|
$params | array |
Generated by Hazaar API Doc Generator on Sun, 01 Mar 2026 09:22:27 +0000