This just posted on stackoverflow.
I recently had trouble with this as I was trying to handle destruction specifically for the case where the server experiences a timeout and I wanted to include class data in the error log. I would receive an error when referencing &$this (although I've seen it done in a few examples, possibly a version issue or a symfony side-effect), and the solution I came up with was fairly clean:
class MyClass
{
protected $myVar;
/**
* constructor, registers shutdown handling
*/
public function __construct()
{
$this->myVar = array();
// workaround: set $self because $this fails
$self = $this;
// register for error logging in case of timeout
$shutdown = function () use (&$self) {
$self->shutdown();
};
register_shutdown_function($shutdown);
}
/**
* handle shutdown events
*/
public function shutdown()
{
$error = error_get_last();
// if shutdown in error
if ($error['type'] === E_ERROR) {
// write contents to error log
error_log('MyClass->myVar on shutdown' . json_encode($this->myVar), 0);
}
}
...
Hope this helps!
ARCHIVE NOTICE
My website can still be found at industrialcuriosity.com, but I have not been posting on this blog as I've been primarily focused on therightstuff.medium.com - please head over there and take a look!
Subscribe to:
Post Comments (Atom)
Labels
algorithms
(2)
art
(2)
automation
(8)
aws
(4)
azure
(2)
bamboo
(1)
banking
(2)
blockchain
(1)
books
(5)
c#
(7)
cloud
(4)
collaboration
(5)
comics
(3)
comments
(4)
communication
(6)
crypto
(1)
data ownership
(1)
database
(5)
deployment
(10)
design
(3)
design patterns
(2)
development
(32)
devops
(5)
documentation
(16)
dynamodb
(1)
encryption
(6)
formatting
(4)
git
(1)
golang
(1)
google
(1)
hiring
(1)
html5
(5)
iis
(1)
installation
(5)
integration
(2)
interfaces
(2)
java
(1)
javascript
(11)
lambda
(2)
linux
(5)
mysql
(1)
node.js
(7)
open source
(15)
plugins
(3)
privacy
(2)
processes
(2)
promotions
(1)
python
(4)
readability
(3)
recruitment
(1)
regex
(2)
reviews
(1)
security
(13)
self-publishing
(2)
shakespeare
(3)
sql server
(1)
tattoos
(2)
tools
(21)
user experience
(1)
version control
(3)
virtualization
(1)
No comments:
Post a Comment