I have an php object which I want to be accessible whenever I want. I want to create this object once and keep it persistent so that I don’t have to reload it again. Also I would like this object to be accessible in my controllers/models etc.
Can this be achieved in CodeIgniter/PHP without using session?
Or maybe using session, but will I be able to access its methods?
This object is created using PHP COM library from my dll file
Here is sample of my object:
$myDllObject = new COM("MyDLL.MyCLass");
PHP has an execution model where each web request from the host environment generates a new, fresh PHP environment which is destroyed when the request is completed.
So the simple answer is no, you cannot create an object and persist it between requests. You must “persist” the object in some other way:
I don’t know much about COM, but since COM objects can be created outside the running process I suspect that there is probably some (not-PHP specific) method of connecting to an existing one rather than creating a new one. (This is essentially option 2 above, using the COM services as the IPC.)
A little digging through the PHP COM library docs reveals the
com_get_active_object()function, which may bring you to a working solution. However, you will probably need to learn more about COM from non-PHP sources. Also, read the big fat warning about using a single COM object concurrently:This suggests to me that creating a singleton COM object shared among all requests is actually what you don’t want to do!
If that doesn’t work, PHP has an object serialization method that allows you to serialize the running state of an object to a string and deserialize it back again to the same state. You can customize this by adding the
__sleep()and__wakeup()methods to your class. This mechanism has it’s share of quirks and I don’t know how well the PHP COM library proxy objects would support it.