The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started


Uri Schemes

Uri manipulation

Uri Parts API


Upgrading Guide

The Port component

The library provides a Port class to ease port manipulation.


Using the default constructor.


public function __contruct(int $port = null)

The constructor accepts:



use League\Uri\Components\Port;

$port = new Port(443);
$port->getContent();           //return (int) 443
echo $port;                    //display '443'
echo $port->getUriComponent(); //display ':443'

$string_port = new Port('443');
$string_port->getContent();           //return (int) 443
echo $string_port;                    //display '443'
echo $string_port->getUriComponent(); //display ':443'

$empty_port = new Port();
$empty_port->getContent();           //return null
echo $empty_port;                    //display ''
echo $empty_port->getUriComponent(); //display ''

If the submitted value is not a valid port number an InvalidArgumentException exception is thrown.

On instantiation the submitted string is normalized using RFC3986 rules.

Using a League Uri object

You can acces a Port object with an already instantiated League Uri object.


use League\Uri\Schemes\Http as HttpUri;

$uri  = HttpUri::createFromString('');
$port = $uri->port; // $port is a League\Uri\Components\Port object;

Properties and Methods

The component representation, comparison and manipulation is done using the package UriPart and the Component interfaces methods.


Since version 4.2 this method is deprecated please use Port::getContent instead.

Return an integer if the port was defined or null otherwise.


use League\Uri\Components\Port;

$port = new Port(81);
$port->toInt(); //return (int) 81

$empty_port = new Port();
$empty_port->toInt(); //return null