Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89071 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1411 invoked from network); 5 Nov 2015 02:41:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Nov 2015 02:41:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=bishop.bettini@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bishop.bettini@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.44 as permitted sender) X-PHP-List-Original-Sender: bishop.bettini@gmail.com X-Host-Fingerprint: 74.125.82.44 mail-wm0-f44.google.com Received: from [74.125.82.44] ([74.125.82.44:37779] helo=mail-wm0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 57/00-00752-3D1CA365 for ; Wed, 04 Nov 2015 21:41:24 -0500 Received: by wmll128 with SMTP id l128so1602926wml.0 for ; Wed, 04 Nov 2015 18:41:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:sender:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=LaX7ToiqxNLhu/QkmFk08I4v6Fa3+iCcrWasydKNZn4=; b=lTsTMj1O9bvGdPeWUtAcpOyHjkoKkj3KubeNcx3ksF4dbQ1ES3vU23unmJ7ixGFmEr BcPdZPDV2flwmnvGpz56k1/HvI/J5gR34iZIFzu0abPwUyzLqb722iEi2utur7jcmUNf yvtLl1ps98xGjjN7dNMg3L/MR+ygNOA+v1G09FA0Z/2992Rvvfq6yNvyogj45fwHHkE6 HLjXtRUr+KDW4lWbng4aeeJAxmgaW2GYnSxe8lUiZyEQ4La+1X26sf2S4HhZKF1ZgtTv IQ86roRyEujSCxy5MiLqFE4WLfbUen5u3hCuwfBzF5kAmQU6VttfMfJxbYc1IRKdtJPO gYMg== MIME-Version: 1.0 X-Received: by 10.28.132.18 with SMTP id g18mr426178wmd.64.1446691280804; Wed, 04 Nov 2015 18:41:20 -0800 (PST) Reply-To: bishop@php.net Sender: bishop.bettini@gmail.com Received: by 10.194.42.228 with HTTP; Wed, 4 Nov 2015 18:41:20 -0800 (PST) In-Reply-To: <2680231.U9fny6VzaI@mcmic-probook> References: <3187501.to7rK1FKUl@mcmic-probook> <2680231.U9fny6VzaI@mcmic-probook> Date: Wed, 4 Nov 2015 21:41:20 -0500 X-Google-Sender-Auth: 5S2hcgGvcHoMDlGxEgJWaUAR2YA Message-ID: To: =?UTF-8?Q?C=C3=B4me_Chilliet?= Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11444ce0e462970523c20f7b Subject: Re: [PHP-DEV] One last ldap_connect headache From: bishop@php.net (Bishop Bettini) --001a11444ce0e462970523c20f7b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, Nov 3, 2015 at 11:13 PM, C=C3=B4me Chilliet wro= te: > I got mail from someone saying that in previous version, calling > ldap_connect($host, NULL) would use default port. > While now it is considered as trying to use port 0 and trigger an error. > I believe the current behavior (interpret as zero and trigger error) is correct. > I=E2=80=99m a bit troubled about it because the documentation says: > resource ldap_connect ([ string $hostname =3D NULL [, int $port =3D 389 = ]] ) > So $port defaults to 389 and not to NULL, and it says nothing about > accepting NULL as second parameter. > Let's compare to another PHP function that takes a numeric optional parameter with a non-zero default: array array_unique ( array $array [, int $sort_flags =3D SORT_STRING ] ) Note that SORT_STRING =3D=3D=3D 2, SORT_REGULAR =3D=3D=3D 0. If you pass null for the second argument, you get the SORT_REGULAR behavior, not the default SORT_STRING behavior: print_r(array_unique([ 1, '1', '1a' ], null)); That said, it does seem handful to be able to pass NULL to ask for default > port without remembering which one it is. > The context is something like: > $port =3D NULL; > if (isset($options['port'])) { > $port =3D $options['port']; > } > $resource =3D ldap_connect($host, $port); > A few reasons I'd offer as arguments against this. $port is deprecated, so why add features to deprecated arguments? Other PHP internal functions don't behave this way (array_unique, socket_create_listen, ssh2_connect, etc.), so why go against the grain? Why not document (in a comment) the preferred way of doing this, which might be: if (isset($options['port'])) $resource =3D ldap_connect($host, $options['port']); else $resource =3D ldap_connect($host); > Right now it either needs an if statement or hardcoding 389 instead of > NULL as the default. In the C code we use the constant LDAP_PORT for this > but there is no such thing in PHP. > Any ideas/comments about this? > A PHP user-land constant like LDAP_DEFAULT_PORT might help users out marginally, but to my knowledge no other port numbers are exposed as such in PHP. Like ssh2_connect, for example, uses a raw 22. If one wanted truly robust (paranoid) code, you'd probably want to do: $port =3D getservbyname('ldap', 'tcp'); if (isset($options['port']) && is_numeric($options['port'])) $port =3D intval($options['port']); $resource =3D ldap_connect($host, $port); And this could go in a comment and solve the whole issue without code changes. --001a11444ce0e462970523c20f7b--