Re: Why does `setcookie` send header before throwing `Error` exception?

[email protected] (JEFFRY KILLEN)
Newsgroups php.general
Message-ID <[email protected]>

> On Apr 4, 2021, at 4:39 PM, JEFFRY KILLEN <[email protected]> wrote:
> 
> 
> 
>> On Apr 4, 2021, at 3:07 PM, Jair López <[email protected]> wrote:
>> 
>> Sorry for the typos in my previous message. I stand corrected, it is
>> 
>>> 	setcookie('name', 'value', 1, new Foo());
>> 
>> instead of
>> 
>>> 	setcookie('name', 'value', 1, => new Foo());
>> 
>> and here is the fixed test file:
>> 	--TEST--
>> 	setcookie() sends header before throwing `Error` exception
>> 	--FILE--
>> 	<?php
>> 	ob_start();
>> 
>> 	class Foo{
>> 	}
>> 
>> 	try {
>> 		setcookie('name', 'value', array(
>> 			'expires' => 1,
>> 			'path' => new Foo()
>> 		));
>> 	} catch (\Error $e) {
>> 		echo $e->getMessage() . "\n";
>> 	}
>> 
>> 	var_dump(headers_list());
>> 	echo "Done\n";
>> 	?>
>> 	--EXPECTHEADERS--
>> 
>> 	--EXPECTF--
>> 	Object of class Foo could not be converted to string
>> 	array(1) {
>> 	[0]=>
>> 	   string(23) "X-Powered-By: PHP/8.0.3"
>> 	}
>> 	Done
> 
> 
> Hello;
> This may not make the difference but the line:
>> 'path' => new Foo() 
> ‘path’=>needs a string, so how is ‘new foo()’
> returning a string?
> 
> Could it be sending the header and then choking
> because ‘path’ doesn’t have a valid value assignment?

As a matter of fact, as you have the class def it looks
like calling new foo() from within the class def would
make it recursively trying to set the cookie.
JK
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.