Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95031 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80902 invoked from network); 11 Aug 2016 01:44:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Aug 2016 01:44:52 -0000 Authentication-Results: pb1.pair.com header.from=bishop.bettini@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bishop.bettini@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.52 as permitted sender) X-PHP-List-Original-Sender: bishop.bettini@gmail.com X-Host-Fingerprint: 209.85.218.52 mail-oi0-f52.google.com Received: from [209.85.218.52] ([209.85.218.52:36792] helo=mail-oi0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 61/32-61523-398DBA75 for ; Wed, 10 Aug 2016 21:44:52 -0400 Received: by mail-oi0-f52.google.com with SMTP id f189so83091485oig.3 for ; Wed, 10 Aug 2016 18:44:51 -0700 (PDT) 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:from:date :message-id:subject:to:cc; bh=BrQDtnOgzfnsfiyuFFlxNK3Ut2cVLAPwIYm6N8fxtHU=; b=uC3H1nFnKdr381OUZ2p92bxdw/HwIRoDsEY9cXsCttENOT7Hx1oJZPEj3Nze4qj4Ol 1ILZXWJoxirdQQrlECgNV9227Z3SUKQRQRSzjBlQCJSFyc4G9YuWlpmRADAJugmjIpfF 3LV8kG50sdOU0XyxmEasVI8Ed/ykSfs2Bn5x0kEGvYlkgpP17e1X86FZF7R6GINs8Avy Fd+LnaiYQ9bevrmWbeDppSgnJB8Xq4t2L8a1E3QbyTe7iPOFs8S0j+HeBVu5pCGLhRdl WoRdmffVr2khuNXbwvT2VCBc43Px83nFOwlkURoqWg1EOmIaEXWkoTyY7rS2ZYP+FJO2 CZZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:reply-to:sender:in-reply-to :references:from:date:message-id:subject:to:cc; bh=BrQDtnOgzfnsfiyuFFlxNK3Ut2cVLAPwIYm6N8fxtHU=; b=QpGU2s6dLOW1MKUF+ctDTEAHKL4Qmjcb82OXESyVYA/nk8fhuWTKeTGSPt++1j3Ugf gUBBUzqb9gUGaxQOc4iMvJOFDy5o0M4i9FPm6BDeiS1mKjCzmaUHRbTG7ivq+cyglx2l iLQLsy9n7Qyeoyvx9S+ulCYwYrbkRYLhmFYMdh162+tBxIplSWaGZ3f7cSDONjadq5DV KUwSuzhYq8a23Rak8HLi8lwrV/o6CwQTfpY/5gFzZsnW0whWLmhtx+hnGszSG6mTZdPb pXm1E/qOWMPGxPQ45xSzTCfx28quAJQRdUuSKptdde5+HvjovQyMNH33rrkwpefrsjr5 KTzA== X-Gm-Message-State: AEkoouu0l3udMxRl0wpB5oE26ZRmOBfAcQdgBMbXvLz7za9udJZHoA3C33CmTVhJuUIRWI6LGcQrwbSgqXNkhg== X-Received: by 10.202.80.4 with SMTP id e4mr3533633oib.51.1470879889389; Wed, 10 Aug 2016 18:44:49 -0700 (PDT) MIME-Version: 1.0 Reply-To: bishop@php.net Sender: bishop.bettini@gmail.com Received: by 10.157.11.214 with HTTP; Wed, 10 Aug 2016 18:44:19 -0700 (PDT) In-Reply-To: <10fbcb03-5de8-4d9a-da1c-7e2bf77937cb@lsces.co.uk> References: <10fbcb03-5de8-4d9a-da1c-7e2bf77937cb@lsces.co.uk> Date: Wed, 10 Aug 2016 21:44:19 -0400 X-Google-Sender-Auth: Bnryxxmn_6s1Ksojp2eAbp-2foA Message-ID: To: Lester Caine Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113d7514507fab0539c1e937 Subject: Re: [PHP-DEV] Simple variable handling. From: bishop@php.net (Bishop Bettini) --001a113d7514507fab0539c1e937 Content-Type: text/plain; charset=UTF-8 On Wed, Aug 10, 2016 at 4:07 PM, Lester Caine wrote: > People keep complaining that I do not contribute any proposals to > improve PHP, which to some extent s correct. Except the one thing that I > keep trying to get a handle on is tidying validating of the basic > variables that are the heart of PHP. > > validate_var_array() is a case in point, since ALL it should do is > handle an array of simple variables for which we can define REAL > validation rules rather than just a very restricted 'type' rule. > Massaging the way the content of a variable is presented is another part > of the basic functions of handling a variable, and simply providing an > escape option which can be set as part of the variable rules set > eliminates the need for 'New operator (short tag) for context-dependent > escaping' and similar tangential matters. If we have a set of rules > wrapping a variable then everything else just follows on, and the SQL > domain model allows a group of variables to take an identical se of rules. > > These are the sorts of things any decent user world library can and does > provide, but if the clock was rolled back prior to all the trouble > created by 'strict typing' and we started again with a more well defined > simple variable I'm sure that much of the conflict could have been > resolved by allowing full validation checks to control an error or > exception depending on the 'style' of PHP a programmer prefers. > > If a function is going to return a variable and that variable has under > the hood a set of validation rules, then one can return an error if the > result is faulty. Or even allow a NULL return if a valid answer is not > available ... if that is the style of programming one prefers. > Exceptions handle unmanaged errors, while proper program flow handles > managed ones! > > Wrap these intelligent variables inside a class and one can create more > powerful objects but ones which still use all the basic functionality. > Similarly an array of them can be asked to provide a simple 'yes/no' if > all of the variables pass their validation check, or an array of > elements which need processing. > Do you mean attaching a functional validator to a variable, something like this hypothetical code? (Note the 3rd argument to settype): // $_POST['age'] = 27; // $_POST['name'] = 'Sugah Pop'; try { settype($_POST['age'], 'int', 'is_int'); settype($_POST['name'], 'string', function ($name) { return strlen($name) < 255; }); } catch (\TypeError $er) { die($er->getMessage()); } // ... Later: $_POST['name'] = str_repeat('a', 1024); // Throws \TypeError: "Invalid value set into variable in demo.php on line 14" Or same idea, but used as "smarter" formal argument validators: function do_something($age, $name) { settype($age, 'int', 'is_int'); settype($name, 'string', function ($name) { return strlen($name) < 255; }); // $age and $name now have persistent validator rules attached to them // write operations onto the variable will assert the truth of the // validator before assigning. Obviously, there is a run-time cost. } --001a113d7514507fab0539c1e937--