Discussion:
Fatal error: Cannot re-assign $this -- any plans to "fix" this limitation?
Daevid Vincent
2014-01-13 22:53:47 UTC
Permalink
Are there any plans to fix this "bug" (or add this as a feature depending on
your POV)

I have a connection class that uses singletons for each database. We have
replication on our PROD boxes but on our VMs we don't, so the code does a
little magic to determine if it's an INSERT|UPDATE|DELETE|REPLACE and tries
to change to the proper mysql connection (write to master, read from slaves
in the case of PROD and just use the same DB on the VM).

The problem, and it's quite frustrating considering it doesn't make a lot of
logical sense why you couldn't do it, is that PHP doesn't allow the
re-assignment of $this. :-\

Both of these fail (of course)

$this = Connection::get_instance(self::_determine_RDBMS($mybase[1], true));

$this = self::$_instance_array[$this->_base];

It seems that it should be code-wise do-able by simply having the guts of
PHP (the C/C++ code PHP is created with) create a new object - just as it
did when creating $this, then just change the pointer that $this is looking
at to the new object, throw the old object onto the heap for cleanup. Why is
that so difficult?
Aziz Saleh
2014-01-13 23:18:15 UTC
Permalink
Post by Daevid Vincent
Are there any plans to fix this "bug" (or add this as a feature depending on
your POV)
I have a connection class that uses singletons for each database. We have
replication on our PROD boxes but on our VMs we don't, so the code does a
little magic to determine if it's an INSERT|UPDATE|DELETE|REPLACE and tries
to change to the proper mysql connection (write to master, read from slaves
in the case of PROD and just use the same DB on the VM).
The problem, and it's quite frustrating considering it doesn't make a lot of
logical sense why you couldn't do it, is that PHP doesn't allow the
re-assignment of $this. :-\
Both of these fail (of course)
$this = Connection::get_instance(self::_determine_RDBMS($mybase[1], true));
$this = self::$_instance_array[$this->_base];
It seems that it should be code-wise do-able by simply having the guts of
PHP (the C/C++ code PHP is created with) create a new object - just as it
did when creating $this, then just change the pointer that $this is looking
at to the new object, throw the old object onto the heap for cleanup. Why is
that so difficult?
https://bugs.php.net/search.php (Type: Feature/Change Request).

As to be able to dynamically change $this (which in PHP is a reference to
the calling object), I personally do not think it is a good idea.
Particularly if more than one person is working on the file/project - you
expect $this to be something, have specific methods, but ends up being a
different object.
Daevid Vincent
2014-01-14 19:26:44 UTC
Permalink
-----Original Message-----
Sent: Monday, January 13, 2014 3:18 PM
To: Daevid Vincent
Subject: Re: [PHP] Fatal error: Cannot re-assign $this -- any plans to "fix"
this limitation?
Post by Daevid Vincent
Are there any plans to fix this "bug" (or add this as a feature depending on
your POV)
I have a connection class that uses singletons for each database. We have
replication on our PROD boxes but on our VMs we don't, so the code does a
little magic to determine if it's an INSERT|UPDATE|DELETE|REPLACE and
tries
Post by Daevid Vincent
to change to the proper mysql connection (write to master, read from
slaves
Post by Daevid Vincent
in the case of PROD and just use the same DB on the VM).
The problem, and it's quite frustrating considering it doesn't make a lot of
logical sense why you couldn't do it, is that PHP doesn't allow the
re-assignment of $this. :-\
Both of these fail (of course)
$this = Connection::get_instance(self::_determine_RDBMS($mybase[1],
true));
Post by Daevid Vincent
$this = self::$_instance_array[$this->_base];
It seems that it should be code-wise do-able by simply having the guts of
PHP (the C/C++ code PHP is created with) create a new object - just as it
did when creating $this, then just change the pointer that $this is
looking
Post by Daevid Vincent
at to the new object, throw the old object onto the heap for cleanup. Why is
that so difficult?
https://bugs.php.net/search.php (Type: Feature/Change Request).
As to be able to dynamically change $this (which in PHP is a reference to
the calling object), I personally do not think it is a good idea.
Particularly if more than one person is working on the file/project - you
expect $this to be something, have specific methods, but ends up being a
different object.
I don’t need it to change to an entirely different object class, just a different instance of the same class. I agree that changing $this within itself to something completely foreign would be horrible. But changing to a new/different version of the same thing (same methods, etc.) seems logical and as illustrated useful.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dan Munro
2014-01-14 19:34:38 UTC
Permalink
This seems like a problem that would more easily be solved by reorganizing
your code. I'm not an expert, and you only posted a small snippet of
code, so please take what I say with the appropriate measure of salt.

That being said, I can't possibly imagine a case where reassigning $this
would do anything but horribly confuse the implementation of your code. You
mentioned singletons, I would assume Connection::get_instance() would
return the same connection object for each connection type, not try to
modify itself to take on the properties of the requested connection type.
Does that make any sense? Maybe I would understand better if you could post
a more complete snippet of relevant code.
Post by Daevid Vincent
-----Original Message-----
Sent: Monday, January 13, 2014 3:18 PM
To: Daevid Vincent
Subject: Re: [PHP] Fatal error: Cannot re-assign $this -- any plans to
"fix"
this limitation?
Post by Daevid Vincent
Are there any plans to fix this "bug" (or add this as a feature
depending
Post by Daevid Vincent
on
your POV)
I have a connection class that uses singletons for each database. We
have
Post by Daevid Vincent
replication on our PROD boxes but on our VMs we don't, so the code
does a
Post by Daevid Vincent
little magic to determine if it's an INSERT|UPDATE|DELETE|REPLACE and
tries
Post by Daevid Vincent
to change to the proper mysql connection (write to master, read from
slaves
Post by Daevid Vincent
in the case of PROD and just use the same DB on the VM).
The problem, and it's quite frustrating considering it doesn't make a
lot
Post by Daevid Vincent
of
logical sense why you couldn't do it, is that PHP doesn't allow the
re-assignment of $this. :-\
Both of these fail (of course)
$this = Connection::get_instance(self::_determine_RDBMS($mybase[1],
true));
Post by Daevid Vincent
$this = self::$_instance_array[$this->_base];
It seems that it should be code-wise do-able by simply having the guts
of
Post by Daevid Vincent
PHP (the C/C++ code PHP is created with) create a new object - just as
it
Post by Daevid Vincent
did when creating $this, then just change the pointer that $this is
looking
Post by Daevid Vincent
at to the new object, throw the old object onto the heap for cleanup.
Why
Post by Daevid Vincent
is
that so difficult?
https://bugs.php.net/search.php (Type: Feature/Change Request).
As to be able to dynamically change $this (which in PHP is a reference to
the calling object), I personally do not think it is a good idea.
Particularly if more than one person is working on the file/project - you
expect $this to be something, have specific methods, but ends up being a
different object.
I don’t need it to change to an entirely different object class, just a
different instance of the same class. I agree that changing $this within
itself to something completely foreign would be horrible. But changing to a
new/different version of the same thing (same methods, etc.) seems logical
and as illustrated useful.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
From the desk of Dan Munro
Jasper Kips
2014-01-14 19:57:06 UTC
Permalink
Daevid,
Please read this bug report. It explais why you can't do what you want to do. And, I think it will not change


Jasper
Verstuurd vanaf mijn iPad
Post by Daevid Vincent
-----Original Message-----
Sent: Monday, January 13, 2014 3:18 PM
To: Daevid Vincent
Subject: Re: [PHP] Fatal error: Cannot re-assign $this -- any plans to "fix"
this limitation?
Post by Daevid Vincent
Are there any plans to fix this "bug" (or add this as a feature depending on
your POV)
I have a connection class that uses singletons for each database. We have
replication on our PROD boxes but on our VMs we don't, so the code does a
little magic to determine if it's an INSERT|UPDATE|DELETE|REPLACE and
tries
Post by Daevid Vincent
to change to the proper mysql connection (write to master, read from
slaves
Post by Daevid Vincent
in the case of PROD and just use the same DB on the VM).
The problem, and it's quite frustrating considering it doesn't make a lot of
logical sense why you couldn't do it, is that PHP doesn't allow the
re-assignment of $this. :-\
Both of these fail (of course)
$this = Connection::get_instance(self::_determine_RDBMS($mybase[1],
true));
Post by Daevid Vincent
$this = self::$_instance_array[$this->_base];
It seems that it should be code-wise do-able by simply having the guts of
PHP (the C/C++ code PHP is created with) create a new object - just as it
did when creating $this, then just change the pointer that $this is
looking
Post by Daevid Vincent
at to the new object, throw the old object onto the heap for cleanup. Why is
that so difficult?
https://bugs.php.net/search.php (Type: Feature/Change Request).
As to be able to dynamically change $this (which in PHP is a reference to
the calling object), I personally do not think it is a good idea.
Particularly if more than one person is working on the file/project - you
expect $this to be something, have specific methods, but ends up being a
different object.
I don’t need it to change to an entirely different object class, just a different instance of the same class. I agree that changing $this within itself to something completely foreign would be horrible. But changing to a new/different version of the same thing (same methods, etc.) seems logical and as illustrated useful.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...