1

I need to create a global object that can be reachable from my script's other php files.

My point is, create only one COM object when script / server started. And this COM object will wait for my commands to to some processing work.

I've tried to write a class to make it happen but, when i refresh page, my class reloads and my static variables are re-defining.

Is there any code or any class or way to solve this problem?

Thank you all.

8
  • 1
    PHP doesn't maintain state between page requests (such as a page refresh) unless you maintain it in $_SESSION; and I don't believe that a COM object can be maintained in session. A possible alternative might be to create a daemon process, always running on the server that maintains your COM object, and then make requests from your PHP script to the daemon process
    – Mark Baker
    Jun 27, 2013 at 17:20
  • Nope, that's not what i actually want. My point is, I'll create a COM object (DLL connection) in a differend php file. This file'll wait my commands to make some process. For example, COM.php file is running and waiting my command. test.php is my php file to send commands to COM object that i've created in COM.php file. Jun 27, 2013 at 17:24
  • You may use APC or memcache to store objects in memory. php.net/apc_store
    – jgb
    Jun 27, 2013 at 17:24
  • @jgb - note that objects stored in APC or memcache are serialized in the same way as objects stored in $_SESSION; and it isn't possible to serialize a reference to a resource
    – Mark Baker
    Jun 27, 2013 at 17:25
  • thanks. I understand that. So, how can I manage that COM object (DLL) connection the way that i mean ? I've to open that connection once and send it requests and receive the returned values like string, object, int etc. (Sorry about my English by the way) Jun 27, 2013 at 17:38

1 Answer 1

0

If you're outside of a function, you can define it. Once you are in a function, you use the global keyword to "pull it into" the function.

Example:

<?php
$foo = "bar";
echo $foo; // bar

function test1() {
  echo $foo; // error
}
test1();

function test2() {
  global $foo;
  echo $foo; // bar
}
test2();
?>
1
  • That's not what the OP's asking for. He wants an object that persists across multiple script invocations.
    – Marc B
    Jun 27, 2013 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.