I would like to create my first RFC proposing adding a new operator
!instanceof
named T_NOT_INSTANCEOF
.
The purpose of this RFC is to add syntactic sugar for checking if an object
is not an instance of something else.
The current syntax for checking not instance of looks the following way
!$object instanceof MyClass
When I read this I read it as:
Negate the $object
variable and check if that is an instance of MyClass
.
My proposed operator would allow for the following syntax
$object !instanceof MyClass
This is for me and people I have spoken to a much clearer syntax.
Some arguments on other ways the not instance of can currently be written
!($object instanceof MyClass) // Wrapping in parenthesis
! $object instanceof MyClass // Simply adding spacing
My main problem with these alternative syntaxes is the readability when
added inside an if
condition
if(!($object instanceof MyClass)) { ...
if(! $object instanceof MyClass) { ...
compared to
if($object !instanceof MyClass) { ...
In regards to implementing the feature, I wouldn't mind trying to do that
myself. I believe this change is relatively simple in the parser and AST
and something I should be able to figure out.
What do people think about this? Would love to clarify if needed.
Best regards
Oliver Nybroe (he/him)
Heya,
While I definitely agree with this, and after more than 10 years of PHP I
still have the tendency to write if ($object !instanceof MyClass)
anyway.
Would it be possible, or would it collide with constants to do the
following?
$object === MyClass;
$object !== MyClass;
The reason I'm hoping this would be possible, is that I often have brainlag
trying to write "instanceof" and I either make several typos, or I end up
with "instance" and it takes me an error message to realize I forgot the
"of". The "instanceof" is counterintuitive for me compared to operators.
If this isn't possible and !instanceof
would be adopted, what about the
following in addition to the proposed example?
$object implements MyInterface;
$object !implements MyInterface;
$object extends MyClass;
$object !extends MyClass;
On Mon, Dec 13, 2021 at 11:53 AM Oliver Nybroe olivernybroe@gmail.com
wrote:
I would like to create my first RFC proposing adding a new operator
!instanceof
namedT_NOT_INSTANCEOF
.The purpose of this RFC is to add syntactic sugar for checking if an object
is not an instance of something else.
The current syntax for checking not instance of looks the following way!$object instanceof MyClass
When I read this I read it as:
Negate the$object
variable and check if that is an instance of
MyClass
.My proposed operator would allow for the following syntax
$object !instanceof MyClass
This is for me and people I have spoken to a much clearer syntax.
Some arguments on other ways the not instance of can currently be written
!($object instanceof MyClass) // Wrapping in parenthesis ! $object instanceof MyClass // Simply adding spacing
My main problem with these alternative syntaxes is the readability when
added inside anif
conditionif(!($object instanceof MyClass)) { ... if(! $object instanceof MyClass) { ...
compared to
if($object !instanceof MyClass) { ...
In regards to implementing the feature, I wouldn't mind trying to do that
myself. I believe this change is relatively simple in the parser and AST
and something I should be able to figure out.What do people think about this? Would love to clarify if needed.
Best regards
Oliver Nybroe (he/him)
pon., 13 gru 2021 o 12:10 Lynn kjarli@gmail.com napisał(a):
Heya,
While I definitely agree with this, and after more than 10 years of PHP I
still have the tendency to writeif ($object !instanceof MyClass)
anyway.
Would it be possible, or would it collide with constants to do the
following?$object === MyClass; $object !== MyClass;
The reason I'm hoping this would be possible, is that I often have brainlag
trying to write "instanceof" and I either make several typos, or I end up
with "instance" and it takes me an error message to realize I forgot the
"of". The "instanceof" is counterintuitive for me compared to operators.If this isn't possible and
!instanceof
would be adopted, what about the
following in addition to the proposed example?$object implements MyInterface; $object !implements MyInterface; $object extends MyClass; $object !extends MyClass;
Why not "not" instead? The "!" in front of "i" in "!implements" is almost
not visible which IMO could get easily ignored unintentionally.
Instead constructs like "$foo not implements stdClass" have higher
visibility and are currently a syntax error.
Cheers,
Michał Marcin Brzuchalski
On Mon, Dec 13, 2021 at 12:19 PM Michał Marcin Brzuchalski <
michal.brzuchalski@gmail.com> wrote:
Why not "not" instead? The "!" in front of "i" in "!implements" is almost
not visible which IMO could get easily ignored unintentionally.
Instead constructs like "$foo not implements stdClass" have higher
visibility and are currently a syntax error.Cheers,
Michał Marcin Brzuchalski
Definitely a good idea! I was considering suggesting that as well when
writing the implements/extends down, but wasn't sure if that would be
doable as it would introduce a new keyword as well if I understand the
setup of php correctly.
The "instanceof" is counterintuitive for me compared to operators.
Hmm interesting, might be possible to go that direction by making
T_IS_EQUAL
acceptclass_name_reference
. I like that, biggest concern
with this is what is the difference (if any) between
$object !== MyClass
$object != MyClass
If this isn't possible and
!instanceof
would be adopted, what about the
following in addition to the proposed example?
I definitely agree that those make sense, but I would say that they should
prob. have their own discussion, but would be much easier to get through if
this one would be accepted.
Why not "not" instead? The "!" in front of "i" in "!implements"
Might be my limited knowledge on the subject, but by usingnot
, we are
makingnot
a reserved keyword, which I think should be done really
carefully.
Best regards
Oliver Nybroe (he/him)
On Mon, 13 Dec 2021 at 12:19, Michał Marcin Brzuchalski <
michal.brzuchalski@gmail.com> wrote:
pon., 13 gru 2021 o 12:10 Lynn kjarli@gmail.com napisał(a):
Heya,
While I definitely agree with this, and after more than 10 years of PHP I
still have the tendency to writeif ($object !instanceof MyClass)
anyway.
Would it be possible, or would it collide with constants to do the
following?$object === MyClass; $object !== MyClass;
The reason I'm hoping this would be possible, is that I often have
brainlag
trying to write "instanceof" and I either make several typos, or I end up
with "instance" and it takes me an error message to realize I forgot the
"of". The "instanceof" is counterintuitive for me compared to operators.If this isn't possible and
!instanceof
would be adopted, what about the
following in addition to the proposed example?$object implements MyInterface; $object !implements MyInterface; $object extends MyClass; $object !extends MyClass;
Why not "not" instead? The "!" in front of "i" in "!implements" is almost
not visible which IMO could get easily ignored unintentionally.
Instead constructs like "$foo not implements stdClass" have higher
visibility and are currently a syntax error.Cheers,
Michał Marcin Brzuchalski
Den man. 13. dec. 2021 kl. 12.53 skrev Oliver Nybroe olivernybroe@gmail.com:
What do people think about this? Would love to clarify if needed.
Best regards
Oliver Nybroe (he/him)
I find this to be more a problem to be solved by individual coding
standards and cs tools. The !instanceof syntax is awkward at best, at
least I find it to be so because it is mixing a symbol with a word to
form an operator.
The documentation already states the following[1]:
Use of parentheses, even when not strictly necessary, can often increase readability of the code by making grouping explicit rather than relying on the implicit operator precedence and associativity.
[1] https://www.php.net/manual/en/language.operators.precedence.php
--
regards,
Kalle Sommer Nielsen
kalle@php.net