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:
Using
com_get_active_object()
in a web server context is not always a smart idea. Most COM/OLE applications are not designed to handle more than one client concurrently, even (or especially!) Microsoft Office. You should read » Considerations for Server-Side Automation of Office for more information on the general issues involved.
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.
I'm not very familiar with the PHP COM library but if it's a object couldn't you maintain it's state by creating a object in your base controller and creating it there. Then just extend your other classes from your Base class.
class Base_Controller extends CI_Controller
{
public $myDllObject;
public function __construct()
{
$this->myDllObject = new COM("MyDLL.MyCLass");
}
}
/* End of file MY_Controller.php / / Location: ./application/core/MY_Controller.php */
Example Controller
// ------------------------------------------------------------------------
class YourController extends Base_Controller
{
public function __construct()
{
if (is_object($this->myDllObject))
{
echo 'Yep';
}
else
{
echo 'Nope';
}
}
}
See the CodeIgniter documentation on Creating Libraries.
Once your library is created you can choose to have it autoloaded in config/autoload.php
, or, you may load the library using $this->load->library('yourlib');
If you need to create a new instance of this class on every page request, consider putting the instantiation in the constructor of your controller. Or, create a base controller class and put it in that constructor.