Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:23114 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17548 invoked by uid 1010); 3 May 2006 08:01:46 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 17520 invoked from network); 3 May 2006 08:01:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2006 08:01:45 -0000 X-PHP-List-Original-Sender: dante@vocalspace.com X-Host-Fingerprint: 69.56.193.72 fox02.stravio.com Linux 2.5 (sometimes 2.4) (4) Received: from ([69.56.193.72:43802] helo=fox02.stravio.com) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id CB/D0-04906-09E58544 for ; Wed, 03 May 2006 03:41:05 -0400 Received: from [127.0.0.1] (c-67-177-79-15.hsd1.tx.comcast.net [67.177.79.15]) by fox02.stravio.com (Postfix) with ESMTP id 3500826C31A; Wed, 3 May 2006 02:41:01 -0500 (CDT) Message-ID: <44585E89.6000400@vocalspace.com> Date: Wed, 03 May 2006 02:40:57 -0500 User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 To: Eric Coleman Cc: PHPdev , dante@lorenso.com References: <44584619.60400@lorenso.com> <200605030815.34801.johannes@php.net> <44585828.1030603@vocalspace.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Seeking 'coalesce' php internal function From: dante@vocalspace.com ("D. Dante Lorenso") Eric, This reply is too basic and is not the answer. The problem is more complex then you have grasped. The only way to remove the notice and warning errors is by using pass-by-reference in the userspace function. Pass-by-reference can not be done for literal values and will only work on variables. All notices and warnings have been thrown long before func_get_args can be run. Besides, func_get_args will return an array and then isset() and empty() no longer apply to the original variables. Dante Eric Coleman wrote: > http://us3.php.net/func_get_args > http://us3.php.net/array > > Enjoy. > > Eric Coleman > > > Eric Coleman > > http://aplosmedia.com > home: 412 399 1024 > cell: 412 779 5176 > > > On May 3, 2006, at 3:13 AM, D. Dante Lorenso wrote: > >> Johannes Schlueter wrote: >>> please search the archives for "ifsetor". >>> >> >> I have completed this search and find: >> >> http://marc.theaimsgroup.com/?r=1&w=2&q=b&l=php-dev&s=coalesce >> http://marc.theaimsgroup.com/?l=php-dev&w=2&r=1&s=ifsetor&q=b >> >> I am using PHP 5.1.2 currently and thought using pass-by-reference >> was frowned upon. Assuming a mistaken assumption I move forward and >> see the following code which was presented to the list: >> >> > function ifsetor(&$var, $value) { >> return (isset($var)) ? $var : $value; >> } >> print ifsetor($x, "dante"); >> ?> >> >> This works as I'd like in my dev environment, however, this does not >> solve the problem of allowing variable number of parameters >> (variables and values). So, I've whipped up this 10 variable version: >> >> > function ifsetor(&$var1, &$var2, &$var3, &$var4, &$var5, &$var6, >> &$var7, &$var8, &$var9, &$var10) { >> if (isset($var1)) return $var1; >> if (isset($var2)) return $var2; >> if (isset($var3)) return $var3; >> if (isset($var4)) return $var4; >> if (isset($var5)) return $var5; >> if (isset($var6)) return $var6; >> if (isset($var7)) return $var7; >> if (isset($var8)) return $var8; >> if (isset($var9)) return $var9; >> if (isset($var10)) return $var10; >> return false; >> } >> print ifsetor($x["notset"]->notset, $y["notset"]->notset, $z->notset, >> "dante"); >> ?> >> >> PHP Fatal error: Only variables can be passed by reference in >> .../ifsetor.php on line 15 >> PHP Warning: Missing argument 5 for ifsetor(), called in >> .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 >> PHP Warning: Missing argument 6 for ifsetor(), called in >> .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 >> PHP Warning: Missing argument 7 for ifsetor(), called in >> .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 >> PHP Warning: Missing argument 8 for ifsetor(), called in >> .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 >> PHP Warning: Missing argument 9 for ifsetor(), called in >> .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 >> PHP Warning: Missing argument 10 for ifsetor(), called in >> .../ifsetor.php on line 15 and defined in .../ifsetor.php on line 2 >> >> I can work around passing values by reference by calling the function >> like this: >> >> print ifsetor($x["notset"]->notset, $y["notset"]->notset, >> $z->notset, $temp = "dante"); >> >> But you have to admit that's getting ugly and still, there are >> undefined parameters declared ... so then I tried it by assigning >> default values: >> >> > function ifsetor(&$var1=null, &$var2=null, &$var3=null, &$var4=null, >> &$var5=null, &$var6=null, &$var7=null, &$var8=null, &$var9=null, >> &$var10=null) { >> if (isset($var1)) return $var1; >> if (isset($var2)) return $var2; >> if (isset($var3)) return $var3; >> if (isset($var4)) return $var4; >> if (isset($var5)) return $var5; >> if (isset($var6)) return $var6; >> if (isset($var7)) return $var7; >> if (isset($var8)) return $var8; >> if (isset($var9)) return $var9; >> if (isset($var10)) return $var10; >> return false; >> } >> print ifsetor($x["notset"]->notset, $y["notset"]->notset, $z->notset, >> $temp = "dante")."\n"; >> print_r($x); >> ?> >> >> Now, $x, $y, and $z ARE set ... >> >> dante >> Array ( [notset] => stdClass Object ( [notset] => ) ) >> >> This is a nasty side-effect and can not be accepted. I did not want >> to set these variables, just check their existance. >> >> I am willing to go away and concede that this should be done in >> userspace IF you can show me that it is in fact possible. The 2 >> argument ifsetor is trivial in userspace, yes, but the variable case >> is more useful in a variety of situations and provides the most >> benefit to simplify code. I did not find the answer in the >> archives. Can you point me in the right direction? I need to be >> able to do all of the following: >> >> * variable number of parameters >> * test 'isset' or 'empty' >> * testing variables and non-variable values (pass-by-reference won't >> work on values) >> * not have side-effect of defining values which are not already set >> * not trigger notices or warnings >> * returns value of first proper match >> >> Dante >> >>> johannes >>> >>> On Wednesday 03 May 2006 07:56, D. Dante Lorenso wrote: >>> >>>> All, >>>> >>>> I'm sure this has been asked somewhere, but since I see requests for >>>> features for 5.2 or 6.0, I'd like to add a "simple" item to the list >>>> which would be quite useful to me and would simplify and clean up a >>>> lot >>>> of code out there: >>>> >>>> function coalesce(...) >>>> >>>> This works much like in the SQL version of the same. In SQL, the >>>> function returns the first non-null argument from an arbitrary >>>> list. In >>>> our use, it should return the first non-empty value from the list: >>>> >>>> Example: >>>> >>>> $x = "dante"; >>>> $y = ""; >>>> $z = ""; >>>> >>>> $value = coalesce($z, $y, $x); // $value = "dante" >>>> >>>> This function would ideally be built into the language and bypass >>>> warnings about undefined variables like 'empty' does. It might be >>>> nice >>>> to have several varieties of this function: >>>> >>>> * return first parameter where empty() is FALSE >>>> * return first parameter where isset() is TRUE >>>> >>>> I don't think something like this can NOT be written in userspace >>>> because the 'isset' and 'empty' checks need to be run before arguments >>>> can be passed to a user function or warnings will start flying. A >>>> function like this simplifies code which used to look like this: >>>> >>>> if (!empty($_POST["postkey"])) { >>>> $value = $_POST["postkey"]; >>>> } >>>> elseif (!empty($_GET["getkey"])) { >>>> $value = $_POST["getkey"]; >>>> } >>>> elseif (!empty($default_value)) { >>>> $value = $default_value; >>>> } >>>> else { >>>> $value = "hard coded value"; >>>> } >>>> >>>> Into this: >>>> >>>> $value = coalesce($_POST["postkey"], $_GET["getkey"], >>>> $default_value, "hard coded value"); >>>> >>>> Can this be built and included? >>>> >>>> Dante >>>> >>> >>> >> >> --PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >