Page 1 of 1

suggestion for finding domain

Posted: Sat Mar 31, 2007 2:51 pm
by audrey
The __construct method in Swift.php currently has:

Code: Select all

public function __construct(Swift_IConnection &$object, $domain=false)
	{
		if (!$domain) $domain = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'SwiftUser';
If run from CLI, $_SERVER['SERVER_NAME'] will not exist. I suggest you change to:

Code: Select all

public function __construct(Swift_IConnection &$object, $domain=NULL)
{
	$domain = !is_null($domain)
		? $domain
		: !empty($_SERVER['SERVER_NAME'])
			? $_SERVER['SERVER_NAME']
			: !empty($_SERVER['HOSTNAME'])
				? $_SERVER['HOSTNAME']
				: 'SwiftUser';

Personally, i'm just passing it as the second param to new Swift() but i thought somebody might want to see this.

Posted: Sat Mar 31, 2007 11:58 pm
by Benjamin
The logic will probably fail (see http://us3.php.net/manual/sv/language.o ... .php#41251), and the nested ternary conditions are not clear to someone who wants to go in and read/edit the code. In this case, if statements, or even a seperate function or class would be better.

Posted: Sun Apr 01, 2007 12:12 am
by audrey
A ternary always sounded to me like something that belongs in a nest.

Anyhoo, i think it's eminently readable. Indentation is crucial, of course.

Posted: Sun Apr 01, 2007 12:17 am
by Benjamin
Please see my revised post above, which cites an example.

Posted: Sun Apr 01, 2007 3:28 am
by audrey
Right you are!

And with the added braces, i'd have to agree that it would become unreadable. Otherwise, i have no problem following it.

Posted: Sun Apr 01, 2007 7:29 am
by Chris Corbyn
It's probably a fair trade-off that it's setabble in the constructor anyway. I don't think anything in $_SERVER exists if running on CLI does it?

Posted: Sun Apr 01, 2007 9:29 am
by feyd
$_SERVER is mostly filled with environment variables when run from the command line.

Posted: Sun Apr 01, 2007 11:12 am
by audrey
This will quickly show you:
$ php -r 'phpinfo();' > phpinfo.txt