Currently PHP has a method called "version_compare()". Should be great
if we have the same function but to general usage. There are some
implementations on web for that, but I guess that it could be done
natively.
compare(mixed $a, mixed $b, string $operator): boolean
Then I could do something like that:
compare(1, 2, '>') === false
compare('1', 1, '===') === false
It should be very useful to libraries that expects that user pass an
operator. For instance:
$collection->where('id', '>', 5) => compare($id, 5, '>')
I guess that it could supports all PHP operators: ==, ===, !=, <>,
!==, <, >, <= and >=.
The spaceship operator does the works too for integers, but not should
works if you expects than $a === $b, or when you expects than $a >=
$b.
--
David Rodrigues
Currently PHP has a method called "version_compare()". Should be great
if we have the same function but to general usage. There are some
implementations on web for that, but I guess that it could be done
natively.
Anything written in PHP could be written in C, the question you need
to answer isn't "How?", it's "Why?".
-Sara
Sara Golemon wrote:
Anything written in PHP could be written in C, the question you need
to answer isn't "How?", it's "Why?".
Sure.
In general, I just thinking about how it can be useful to iterate over
values on a library itself.
Laravel, for instance, have method Collection::where(), but it
currently doesn't accepts an operator, although it should be very
useful.
It could be implemented like it:
function where($index, $value, $operator = '==') {
return array_filter($this->array, function ($arrayValue,
$arrayIndex) use ($index, $value, $operator) {
return compare($arrayValue[$index], $value, $operator);
});
}
$collection->where('id', 1, '>');
Currently it could be done by using "filter" and writing an
user-defined function with > usage.
But the problem is when this condition is client-defined, not
developer-defined. For instance:
<select name="compare"> lower, greater, lower-equal, greater-equal,
equal </select>
In this case, I should implements a switch, where a compare() should works fine.
--
David Rodrigues
Sara Golemon wrote:
Anything written in PHP could be written in C, the question you need
to answer isn't "How?", it's "Why?".Sure.
In general, I just thinking about how it can be useful to ...
There are endless number of things that could be useful if included in
core PHP. But almost all of them can also be done in userland PHP.
Code that is included in core needs to be maintained by a small number
of people who have the C skills and will-power to do the maintenance.
Code that is written in PHP is both easier to maintain (as PHP is a
more powerful language than C in most regards), and has a far larger
number of people who can do the maintenance.
So the question is "Why write it in C, when you can write it in PHP?"
cheers
Dan
Currently PHP has a method called "version_compare()". Should be great
if we have the same function but to general usage. There are some
implementations on web for that, but I guess that it could be done
natively.compare(mixed $a, mixed $b, string $operator): boolean
Then I could do something like that:
compare(1, 2, '>') === false
compare('1', 1, '===') === falseIt should be very useful to libraries that expects that user pass an
operator. For instance:$collection->where('id', '>', 5) => compare($id, 5, '>')
I guess that it could supports all PHP operators: ==, ===, !=, <>,
!==, <, >, <= and >=.The spaceship operator does the works too for integers, but not should
works if you expects than $a === $b, or when you expects than $a >=
$b.
See the Comparable RFC for something similar:
https://wiki.php.net/rfc/comparable
--
Stephen