What Standard PHP Library Could Be

Each article criticizing PHP, followed by an explanation of why "$" in variable notation messes up syntax, is sure to tell you about how poorly function naming is in the PHP core and extensions . And if "$" remains in the standard of the language and our hearts forever (if only as a reminder of good old PHP), then it is high time to do something with the naming of functions.





TLDR;

The author proposes his own rules for naming PHP core functions and their distribution by namespaces.





Standard PHP Library

First of all, I'll tell you a little about the terminological confusion that I deliberately introduce in this article.





The Standard PHP Library (SPL) already exists as a standalone extension . SPL provides data structures, exceptions, iterators, and much more, but it has nothing to do with basic functionality such as string manipulation . They, in my opinion, far from the device of the PHP interpreter, simply exist somewhere in the kernel and are, as it were, part of the language standard.





I believe that the basic functions should be separated into a separate extension and the most appropriate name for it would be the Standard PHP Library.





Problems

There are many shortcomings in the naming of functions and, in general, in their organization. Functions for working with strings are a textbook example , so I will consider them below.





Most notable problems:





  1. : «‎str_» (str_split), «‎str» (strrev), «‎substr_» (substr_compare), - (trim);





  2. , , strncmp : «‎str1» «‎str2», strnatcasecmp: «‎string1» «‎string2», substr_compare: «‎haystack» «‎needle»;





  3. — implode explode;





  4. : strtr, strncmp, strpbrk, strrchr ..;





  5. , , substr_replace ;





  6. false , , strrpos;





  7. : soundex, levenshtein ..





, . , . , , — «‎ ».





, html- . , , , .





, , — . , , , , , PHP , . ? , , .





.
  1. PHP 8.2 — ;





  2. PHP 8.3 — , , , ;





  3. PHP 9 — deprecated, , legacy ,





  4. PHP 10 — end of life legacy .





, 7 8 - 5 , , 10 .





. — .





, , . , , . «‎\», .





- PSR-4. «‎Php», . , «‎Php\Spl».





: PSR-12 .





  1. - , . ,   «‎str_» «‎str», .





  2. lowerCamelCase.





  3. . .





  4. . .





  5. , , .





. sscanf , null. , .





  IDE , .





  1. .





  2. . «‎mixed» .





  3. union- nullable-.





  4. «‎null» , .





  5. .





proof of concept , , , — https://github.com/marvin255/php-spl-proposal/blob/master/src/StringUtilities.php.





— .





. , , : , IDE , cms.





.





strnatcmp(“test1”, “test2”);
//  
StringUtilities::compareUsingNaturalOrder(“test1”, “test2”);
      
      



str_contains(”test”, “t”);
//  
StringUtilities::contains(”test”, “t”);
      
      



, , , . , , — .





, json , , . , , — . .





- . , , PHP.





. , , PHP 7 8, .





With my article, I just wanted to provoke a dialogue and plant the seeds of doubt. Someone has to do this, and I'm sure it is the community that should push the maintainers to think that these changes are inevitable.








All Articles