-1

I run always into the problem that my logic mind says "I search what where" and I bet this is with most developers.

However, the string search function of PHP strpos says "I search where what" which is not intuitive.

strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int

Question:

Is there any way to change (override) the core function?


Bonus questions (which are more directed to developers who work/ed on PHP):

A. What was the reason for the existing order of the parameters?

B. Is there any chance that the main PHP developers consider changing this in one of the future PHP versions? Or adding a modified version of the strpos() to the core.

Avatar
  • 14,622
  • 9
  • 119
  • 198
  • 3
    The problem is if you change the order of the parameters - how would it know which is the needle string and which the haystack string. I think it's just a case of getting used to it. Writing your own version also has the chance of confusing other programmers as they may not know what you mean but know what the standard should do. – Nigel Ren Jan 14 '20 at 09:52
  • 1
    Please open different questions for different questions. Additionally, the first question has been answered on https://stackoverflow.com/questions/3620659/is-it-possible-to-overwrite-a-function-in-php - the other questions should be target to PHP developers directly, for example on a developer mailing list. But as changing the order of arguments would break all existing usages, I'd assume that this has no chance – Nico Haase Jan 14 '20 at 09:52
  • you can override the function – Flash Thunder Jan 14 '20 at 09:52
  • ... or just not use it - I honestly can't remember the last time I used `strpos()` – CD001 Jan 14 '20 at 09:53
  • 1
    _"Is there any chance that the main PHP developers consider changing this"_ - How would we know that? However, I don't see it as likely since that would completely break backwards compatibility, causing more harm than good. – M. Eriksson Jan 14 '20 at 09:56
  • no luck, [php named function parameters](https://wiki.php.net/rfc/named_params) is asleep... – jibsteroos Jan 14 '20 at 10:00
  • A: ‍♂️ It is what it is because someone decided on it once and it's too late to change it now, because… B: Changing it now will break every piece of existing code out there using this function, so… no. – deceze Jan 14 '20 at 10:05
  • 1
    Your best course of action is to define your own `function offset_of_a_in_b($needle, $haystack, $offset = 0) { return strpos($haystack, $needle, $offset); }`. It's a sufficiently distinct name to not cause confusion to anyone, and it works fine alongside existing code. – deceze Jan 14 '20 at 10:10
  • @deceze ... I suspect somebody decided on the order once, in `C` (`strstr`) back in the 1970s and PHP just went with that ;) – CD001 Jan 14 '20 at 10:15

1 Answers1

1

You can override build-in function by something like this:

<?php
   rename_function('strpos', 'strpos_old');
   override_function('strpos', '$a,$b,$c', 'return strpos_new($a,$b,$c);');
   function strpos_new($a,$b,$c){
      return strpos_old($b,$a,$c);  
   }
?>

PS. It needs PECL apd >= 0.2.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91