rfc:empty_function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:empty_function [2023/10/30 11:56] – created alessandro.a.rosa_gmail.comrfc:empty_function [2023/10/31 16:42] (current) – Under Discussion alessandro.a.rosa_gmail.com
Line 1: Line 1:
-====== PHP RFC: Your Title Here ====== +====== PHP RFC: standard built-in is_empty() function ====== 
-  * Version: 0.1 +  * Version: 0.2 
-  * Date: 2023-10-30+  * Date: 2023-10-31
   * Author: Alessandro Rosa, alessandro.a.rosa@gmail.com   * Author: Alessandro Rosa, alessandro.a.rosa@gmail.com
   * Status: Under Discussion   * Status: Under Discussion
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-So far the goal of the standard function `empty' is, according to the official documentation, to "determine whether a variable is empty"There are kinds of flaws that should be settledin my viewpoint:+The official documentation of the function <php>empty()</php> is at https://www.php.net/manual/en/function.empty.php. All the quotations below have been reported from the online version up to the date of this RFCnamely October 31th 2023.
  
-1) the semantics: the function name is ambiguous about the action, that is, it is not clear whether the goal of empty() function should consist of testing the `emptiness' of input variable or return an empty version of the input variable.+Here the purpose of the function <php>empty()</php> is to `//Determine whether a variable is considered to be empty//'. Before exploring the behavior of this built-in function, we would argue upon the following sentence: `//A variable is considered empty if it does not exist or if its value equals false//'. While we agree on the first part, we could not do about the second half, as it throws an exception about the meaning of `//emptiness//'.
  
-2) the behaviorThe only two issues come from inputing no variable or managing the false/true constant values.+Defining `emptiness' might be very a subtle argument and actually not all the most blasoned online dictionaries succeed in giving a quite insightful definitionFirst, 
 +> Merriam-Webster: //`containing nothing', `not occupied or inhabited'//
  
 +I do not like the usage of the above term `nothing', as this is a generic term which does not mean anything with regard to the relation between containers and contents, which is the core of `emptiness' definition.
 +
 +In this sense, a little better version follows:
 +> Oxford dictionary: `//The condition of being without contents//'
 +
 +But it can be improved further in terms of specialized contents for containers, being actually the habitat of variables in programming languages. Finally, a more refined definition is the next, because it sets up a quality relation in terms of `appropriate' contents.
 +> Dictionary.com: `//the fact or state of containing nothing or of being without the usual or appropriate contents//'.
 +
 +Before our conclusions, I will first resume basic notions about variable declaration; each example is followed by some equivalent statement in C-family languages, as they are pretty clearer about the strict connection between the kind of containers and contents.
 +
 +<PHP>
 +//a container for storing an integer number was instantiated,
 +//named `integer' and set to the constant value 1
 +$integer = 1;
 +
 +//In C-family languages, it will ruled through this instantiation
 +//int <var_name> = 1;
 +</PHP>
 +
 +Emptiness here occurs __only__ when
 +<PHP>$integer;</PHP>
 +is coded.
 +
 +<PHP>
 +//a container for storing an integer number was instantiated,
 +//named `float' and set to the constant value 1.1
 +$float = 1.1;
 +
 +//In C-family languages, it will ruled through this instantiation
 +//double <var_name> = 1.1;
 +</PHP>
 +
 +Analogously, emptiness here occurs __only__ when
 +<PHP>$float;</PHP>
 +is coded. A slightly variegated situation occurs for emptiness of strings:
 +
 +<PHP>
 +//a container for storing a sequence of characters was instantiated
 +//named `string' and then set to something
 +$string = "<any or no character>";
 +
 +//In C-family languages, it corresponds to the pointer-like syntax
 +//char* <var_name> = "<any or no characters>";
 +</PHP>
 +
 +Emptiness here occurs in two cases: when
 +<PHP>$string;</PHP>
 +or
 +<PHP>$string="";</PHP>
 +are coded.
 +
 +We conclude that the definition of `emptiness' changes according to the kind of container. Therefore the second option in the official definition of emptiness in the empty function documentation, namely `//a variable is considered empty if its value equals false//', cannot logically holds, with regard to the essence of the variable container. The constant boolean value `''false''' alludes to the instantiation of a one-byte container, which is filled through the `''false''' constant value, hence the one-byte container cannot be judged as `empty'. Moreover, the same objection holds for the zero (0) input value, according to the official return value of <php>empty()</php> function in php.net, being documented as follows:
 +`//Returns true if var does not exist or has a value that is empty or equal to zero, aka falsey, see conversion to boolean. Otherwise returns false//'.
 +
 +In my viewpoint, the optimal behavior of any emptiness-test function shall rely upon the match between the container and the kind of contents. I resumed here below the flaws that should be eventually settled:
 +
 +1) the **semantics**: the function name is ambiguous about the action, that is, it is not clear whether the goal of <php>empty()</php> function should consist of testing the `emptiness' of input variable or return an empty version of the input variable.
 +
 +2) the **behavior**. The only three issues arise from inputing no variable or managing the false and 0 constant values.
 +
 +The ordinary behavior with all the built-in datatypes has been listed below:
 +
 +<PHP>
 +echo "built-in function `empty' behavior:\n";
 +echo "Stage 1: empty, undeclared or null containers (`empty' function)\n";
 +echo "null value >> empty( null ): ".( empty( null ) ? "Empty" : "Not empty" )."\n";
 +echo "undeclared var >> empty( \$i ): ".( empty( $i ) ? "Empty" : "Not empty" )."\n";
 +//echo "empty input >> empty() : ".( empty() ? "Empty" : "Not empty" )."\n"; //empty input var will let it crash
 +echo "empty string >> empty( \"\" ) : ".( empty( "" ) ? "Empty" : "Not empty" )."\n";
 +echo "empty array >> empty( [] ) : ".( empty( [] ) ? "Empty" : "Not empty" )."\n";
 +
 +$res = fopen( 'file.txt', "w+" );
 +echo "resource \$res = fopen( 'file.txt', \"w+\" );\n";
 +echo "resource >> empty( \$res ) : ".( empty( $res ) ? "Empty" : "Not empty" )."\n";
 +if ( $res !== false ) fclose( $res );
 +
 +echo "Stage 2: non-empty containers (`empty' function)\n";
 +echo "boolean true >> empty( true ) : ".( empty( true ) ? "Empty" : "Not empty" )."\n";
 +echo "boolean false >> empty( false ) : ".( empty( false ) ? "Empty" : "Not empty" )."\n"; // expected return value 0, but 1 is returned
 +echo "constant 0 value >> empty( 0 ) : ".( empty( 0 ) ? "Empty" : "Not empty" )."\n"; // expected return value 0, but 1 is returned
 +
 +$a;
 +echo "instantiated variable \$a but not filled >> empty( \$a ) : ".( empty( $a ) ? "Empty" : "Not empty" )."\n";
 +
 +$i = 1; // I have declared a variable for the test below
 +echo "instanatiated var and filled >> empty( \$i ) : ".( empty( $i ) ? "Empty" : "Not empty" )."\n";
 +
 +echo "non-empty string >> empty( \"something\" ) : ".( empty( "something" ) ? "Empty" : "Not empty" )."\n";
 +echo "non empty array >> empty( [10] ) : ".( empty( [10] ) ? "Empty" : "Not empty" )."\n";
 +</PHP>
 +
 +We remark, to the benefit of readers, that the empty string is namely ''""'', that is, no characters between (double)quotes. The empty space, namely ''" "'', cannot match the definition of emptiness. Contents of string containers are properly characters only (i.e., any kind of symbol); hence, according to the dictionaries definitions listed at the top of this RFC, the emptiness of some string occurs when the given variable container for string storage includes no character in general, either if the statements
 +
 +<php>$string;</php>
 +
 +or 
 +
 +<php>$string = "";</php>
 +
 +are coded.
  
 ===== Proposal ===== ===== Proposal =====
-First, the `emptyname shall be adequated to the `is_family of php built-in functions (is_null, is_bool, is_string, is_array, ...). Then I propose `is_empty'.+I understand that this proposal could represent a huge break, anyway I believe that my points above shall not be disregarded in order to strengthen parts of PHP that look flawed or fuzzy so far. Honestly, I still read tons of people out there mocking up PHP for its non-typified nature. Also consider that 1) __semantics__, as you know, is essential for languages. 2) the function __behavior__ match the purposes declared in its name as well as it must cover all cases as possible, without exceptions. 
 + 
 +First, the <php>empty()</php> name shall be adequated to the <php>is_*()</php> family of php built-in functions (<php>is_null()</php><php>is_bool()</php><php>is_string()</php><php>is_array()</php>, ...). Hence I propose <php>is_empty()</php>. The return value will be true for emptiness and false otherwise. If the input variable is not empty would throw an error.
  
-Second, I implemented the following version which fixes the above two flaws:+Second, I implemented the following version which fixes all the above flaws:
  
 +<PHP>
 function is_empty( $input = null ) function is_empty( $input = null )
 { {
- $ser = @serialize( $input ); +     $ser = @serialize( $input ); 
- if ( preg_match( "/^N;$/i", $ser ) === 1 ) return 1; +     if ( preg_match( "/^N;$/i", $ser ) === 1 ) return 1; 
- if ( preg_match( "/^b\:[01]\;?$/i", $ser ) === 1 ) return 1+     if ( preg_match( "/^b\:[01]\;?$/i", $ser ) === 1 ) return 0
- return preg_match( "/0\:(\{\}|\[\]|\"\")\;?$/i", $ser ) === 1 ? 1 : 0;+     return preg_match( "/0\:(\{\}|\[\]|\"\")\;?$/i", $ser ) === 1 ? 1 : 0;
 } }
 +</PHP>
  
-===== Backward Incompatible Changes ===== +Let me first say that I resorted to the <php>serialize()</php> built-in function just for showing here how the function <php>is_empty()</php> should work. The serialization just represents an escamotage in PHP for obtaining a formal basis for eventually checking the input variable emptiness in the examples below. However, I presume that the internal C-implementation would approach emptiness in most proper ways involving memory tests. Please, note that I suppressed warnings in order to let this function run throughout the next tests, that also cover undefined variables: 
-What breaks, and what is the justification for it?+ 
 +<PHP> 
 +echo ">new `is_empty()' function behavior\n"; 
 +echo "Stage 1: empty, undeclared or null containers (`is_empty' function)\n"; 
 +echo "null value >> is_empty( null ): ".( is_empty( null ) ? "Empty" : "Not empty" )."\n"; 
 +echo "undeclared var >> is_empty( \$un ) : ".( is_empty( $un ) ? "Empty" : "Not empty" )."\n"; 
 +echo "empty input >> is_empty() : ".( is_empty() ? "Empty" : "Not empty" )."\n"; 
 +echo "empty string >> is_empty( \"\" ) : ".( is_empty( "" ) ? "Empty" : "Not empty" )."\n"; 
 +echo "empty array >> is_empty( [] ) : ".( is_empty( [] ) ? "Empty" : "Not empty" )."\n"; 
 + 
 +echo "Stage 2: non-empty containers (`is_empty' function)\n"; 
 +echo "boolean true >> is_empty( true ) : ".( is_empty( true ) ? "Empty" : "Not empty" )."\n"; 
 +echo "boolean false >> is_empty( false ) : ".( is_empty( false ) ? "Empty" : "Not empty" )."\n"; 
 +echo "zero (0) constant value >> is_empty( 0 ) : ".( is_empty( 0 ) ? "Empty" : "Not empty" )."\n"; 
 + 
 +$a; 
 +echo "instantiated variable \$a but not filled >> is_empty( \$a ) : ".( is_empty( $a ) ? "Empty" : "Not empty" )."<br/>"; 
 + 
 +$i 1; 
 +echo "instantiated var and filled >> is_empty( \$i ) : ".( is_empty( $i ) ? "Empty" : "Not empty" )."\n"; 
 + 
 +echo "non-empty string >> is_empty( \"something\" ) : ".( is_empty( "something" ) ? "Empty" : "Not empty" )."\n"; 
 +echo "non empty array >> is_empty( [ 10 ] ): ".( is_empty( [10] ) ? "Empty" : "Not empty" )."\n"; 
 + 
 +$res fopen( 'file.txt', "w+" ); 
 +echo "resource \$res fopen( 'file.txt', \"w+\" );\n"; 
 +echo "resource >> is_empty( \$res ) : ".( is_empty( $res ) ? "Empty" : "Not empty" )."\n"; 
 +if ( $res !== false ) fclose( $res ); 
 +</PHP> 
 + 
 +The above code works correctly covers all cases of basic datatypes and, last but not least, can be also finely read.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-List the proposed PHP versions that the feature will be included in.  Use relative versions such as "next PHP 8.x" or "next PHP 8.x.y".+According to the official policies documented at https://wiki.php.net/rfc/releaseprocess, backwards compatibility can only be broken 
 +in a major version, hence the new function <php>is_empty()</php> would appear not before than version 9.
 + 
 +The option of keeping <php>empty()</php> function together with ''is_empty()'' is fine too, so deprecation is not required: backward compatibility would be saved, though I would recommend the usage of ''is_empty()'' however.
  
 ===== RFC Impact ===== ===== RFC Impact =====
 +
 ==== To SAPIs ==== ==== To SAPIs ====
-Describe the impact to CLI, Development web server, embedded PHP etc.+None
  
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
-Will existing extensions be affected?+None
  
 ==== To Opcache ==== ==== To Opcache ====
-It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.+None
  
 Please explain how you have verified your RFC's compatibility with opcache. Please explain how you have verified your RFC's compatibility with opcache.
  
 ==== New Constants ==== ==== New Constants ====
-Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation.+None
  
 ==== php.ini Defaults ==== ==== php.ini Defaults ====
-If there are any php.ini settings then list: +None
-  * hardcoded default values +
-  * php.ini-development values +
-  * php.ini-production values+
  
 ===== Open Issues ===== ===== Open Issues =====
-Make sure there are no open issues when the vote starts!+None
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
-List existing areas/features of PHP that will not be changed by the RFC. +Just the current nomenclature of standard built-in empty() function.
- +
-This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.+
  
 ===== Future Scope ===== ===== Future Scope =====
-This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.+None
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Include these so readers know where you are heading and can discuss the proposed voting options.+We will probably vote for or against adding these functions. This requires 2/3 majority.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Links to any external patches and tests go here. +None
- +
-If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. +
- +
-Make it clear if the patch is intended to be the final patch, or is just a prototype. +
- +
-For changes affecting the core language, you should also provide a patch for the language specification.+
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  +Refer to the above code.
-  - the version(s) it was merged into +
-  - a link to the git commit(s) +
-  - a link to the PHP manual entry for the feature +
-  - a link to the language specification section (if any)+
  
 ===== References ===== ===== References =====
-Links to external references, discussions or RFCs+No refs or links
  
 ===== Rejected Features ===== ===== Rejected Features =====
-Keep this updated with features that were discussed on the mail lists.+None.
rfc/empty_function.1698666989.txt.gz · Last modified: 2023/10/30 11:56 by alessandro.a.rosa_gmail.com