Cookie::__construct( string $name, string $value, array|WpOrgRequestsUtilityCaseInsensitiveDictionary $attributes = array(), array $flags = array(), int|null $reference_time = null )

In this article

Create a new cookie object

Parameters

$namestringrequired
The name of the cookie.
$valuestringrequired
The value for the cookie.
$attributesarray|WpOrgRequestsUtilityCaseInsensitiveDictionaryoptional
Associative array of attribute data

Default:array()

$flagsarrayoptional
The flags for the cookie.
Valid keys are 'creation', 'last-access', 'persistent' and 'host-only'.

Default:array()

$reference_timeint|nulloptional
Reference time for relative calculations.

Default:null

Source

public function __construct($name, $value, $attributes = [], $flags = [], $reference_time = null) {
	if (is_string($name) === false) {
		throw InvalidArgument::create(1, '$name', 'string', gettype($name));
	}

	if (is_string($value) === false) {
		throw InvalidArgument::create(2, '$value', 'string', gettype($value));
	}

	if (InputValidator::has_array_access($attributes) === false || InputValidator::is_iterable($attributes) === false) {
		throw InvalidArgument::create(3, '$attributes', 'array|ArrayAccess&Traversable', gettype($attributes));
	}

	if (is_array($flags) === false) {
		throw InvalidArgument::create(4, '$flags', 'array', gettype($flags));
	}

	if ($reference_time !== null && is_int($reference_time) === false) {
		throw InvalidArgument::create(5, '$reference_time', 'integer|null', gettype($reference_time));
	}

	$this->name       = $name;
	$this->value      = $value;
	$this->attributes = $attributes;
	$default_flags    = [
		'creation'    => time(),
		'last-access' => time(),
		'persistent'  => false,
		'host-only'   => true,
	];
	$this->flags      = array_merge($default_flags, $flags);

	$this->reference_time = time();
	if ($reference_time !== null) {
		$this->reference_time = $reference_time;
	}

	$this->normalize();
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.