Hello everyone. I have googled for days now trying to find out a solution to my problem.
I have a script (firstpage.php) which runs infintely using set_time_limit(0);
I have instantiated a class conn in this script.
Since i have instantiated a class and the script is still running i presume that the properties / data members of the class must be available somewhere. Is there any way to access those i.e. data members / properties of an already instantiated class from another page/ script (say secondpage.php)?
I have tried a few other forums as well but i have no solution so far.
Thanks
acess properties of an already instantiated class
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: acess properties of an already instantiated class
Unless you're running firstpage.php as a command line script, then your set_time_limit(0); is irrelevant. Unless you have a while(true) or similar loop in your script that will keep it running permanently, then it will terminate when it finishes executing. If requested from a browser, then it will be terminated (even if it is in a permanent loop) when the web server times out.
A script runs when requested from the browser, executes in an apache thread (assuming that is your choce of web server) and then terminates, removing itself completely from memory. Nothing it creates (objects, data, database connections) is available to other browser requests, even for the same script.... each runs totally independently, unless you use some very specific tools to share data between them (e.g. sessions, cache data, etc)
A script runs when requested from the browser, executes in an apache thread (assuming that is your choce of web server) and then terminates, removing itself completely from memory. Nothing it creates (objects, data, database connections) is available to other browser requests, even for the same script.... each runs totally independently, unless you use some very specific tools to share data between them (e.g. sessions, cache data, etc)
Re: acess properties of an already instantiated class
Thanks Mark.
I do have the script permanently using a while loop etc.
Since my script is running, the data members of the class should be "somewhere" isn't it? I have defined the data member/property that i need to access as "public". Any ideas?
BTW $_session etc are not getting written since the script does not end. Tried using session_write_close. Didnot help.
Thanks
I do have the script permanently using a while loop etc.
Since my script is running, the data members of the class should be "somewhere" isn't it? I have defined the data member/property that i need to access as "public". Any ideas?
BTW $_session etc are not getting written since the script does not end. Tried using session_write_close. Didnot help.
Thanks
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: acess properties of an already instantiated class
So the script is running as a command line script!codetruck wrote:I do have the script permanently using a while loop etc.
You're going to need to create a method to communicate between the pages, probably using sockets or similar.codetruck wrote:Since my script is running, the data members of the class should be "somewhere" isn't it? I have defined the data member/property that i need to access as "public". Any ideas?
How competent a coder are you? Because socket programming isn't for the faint-hearted.
You'll need the firstpage.php script to Initialise a "listener" for telnet connections or external requests,
Code: Select all
// Initialise the Listener for telnet connections or external requests for information about the
// controller status
$this->_listenerHost = '127.0.0.1';
$this->_listenerPort = 81;
$this->_maxConnections = 5;
if ($this->_controlSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) {
if (socket_set_option($this->_controlSocket, SOL_SOCKET,SO_REUSEADDR, 1)) {
// Bind it to our external controller port
if (socket_bind($this->_controlSocket, $this->_listenerHost, $this->_listenerPort)) {
// Start listening, though we won't actually accept any messages until
// we get into the main processing loop ($this->go())
if ($result = socket_listen($this->_controlSocket, $this->_maxConnections)) {
// Store details of the listener socket in our socket arrays for future reference
$this->_activeSockets = array($this->_controlSocket);
$this->_clientInputBuffer = array('');
$this->_clientInputBufferFull = array(False);
$this->_clientLastCommunication = array(time());
}
}
}
}
Finally, secondpage.php needs modifying to make a call against firstpage.php
Code: Select all
$IPConnection = new IPC($hostIPAddress,$serverListenerPort);
if (!$IPConnection->isErrorSet()) {
$IPConnection->sendMessage('requestData');
$data = $IPConnection->waitForResponse();
$IPConnection->closeConnection();
}
Code: Select all
class IPC extends baseClass
{
// --- ATTRIBUTES ---
/**
* @access private
* @var string
*/
private $_loggerName = 'IPCInterface';
/**
* @access private
* @var string
*/
private $_os = NULL;
/**
* @access private
* @var resource
*/
private $_socket = NULL;
/**
* @access private
* @var boolean
*/
private $_socketConnected = False;
// --- OPERATIONS ---
public function sendMessage($message)
{
socket_write($this->_socket,$message."\n");
} // function sendMessage()
public function waitForResponse()
{
$clientInputBuffer = '';
do {
$clientInputBuffer = $this->_processResponse();
} while ($clientInputBuffer == '');
return $clientInputBuffer;
} // function waitForResponse()
private function _processResponse()
{
$clientInputBufferFull = False;
$clientInputBuffer = '';
while (!$clientInputBufferFull) {
$changedSockets = array($this->_socket);
$num_changed_sockets = socket_select($changedSockets, $write = NULL, $except = NULL, NULL);
$socketErrorCode = 0;
@socket_clear_error($this->_socket);
foreach($changedSockets as $changedSocket) {
// Read the incoming message from the socket
if ($this->_os == 'win32') {
// If we're running on a windows platform we build up the message a character at a time until we get a terminator
// (Carriage Return or Line Feed are the two possible terminators)
$char = socket_read($changedSocket, 1, PHP_BINARY_READ);
$socketErrorCode = socket_last_error($changedSocket);
if ($socketErrorCode == 0) {
if ($char == "\r" || $char == "\n") {
$clientInputBufferFull = True;
} else {
$clientInputBuffer .= $char;
}
}
} else {
// If we're running on a *nix platform we receive the message in a single block with an arbitrary size limit of
// 1024 characters, with Carriage Return or Line Feed as the two possible terminators
$clientInputBuffer = socket_read($changedSocket, 1024, PHP_NORMAL_READ);
$socketErrorCode = socket_last_error($changedSocket);
if ($socketErrorCode == 0) {
$clientInputBufferFull = True;
}
}
// Socket error handler
if ($socketErrorCode <> 0) {
$clientInputBufferFull = False;
}
}
}
return $clientInputBuffer;
} // function _processResponse()
/**
*
* @access constructor
* @param string $serverHostIP IP Address of the server to connect to
* @param integer $serverPort IP Port number to use for the connection
* @param string $connectionName name to use for this connection
* @return void
*
*/
function __construct( $serverHostIP, $serverPort, $connectionName = NULL )
{
$this->_logger = &LoggerManager::getLogger($this->_loggerName);
$this->_logger->info(__CLASS__.'::constructor');
// OS Specific settings
$this->_os = ((strpos(strtolower(PHP_OS), 'win') === 0) || (strpos(strtolower(PHP_OS), 'cygwin') !== false)) ? 'win32' : 'unix';
$this->_logger->info('OS = '.$this->_os);
// Create a connection socket
if (($this->_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === False) {
$this->_logger->info('Failed to create socket');
$this->_setError(socketError,'socket_create()',socket_strerror(socket_last_error()));
return;
}
$this->_logger->info('Created socket');
// Connect socket to our connection server and port
$this->_socketConnected = False;
// Use non-blocking connection so that the connect is immediately set
socket_set_nonblock($this->_socket);
@socket_connect($this->_socket, $serverHostIP, $serverPort);
// Reset to non-blocking so that we can work with it
socket_set_block($this->_socket);
// Use socket_select to trap for errors on the socket_connect, including a timeout trap (5 seconds)
switch(socket_select($r=array($this->_socket), $w=array($this->_socket), $f=array($this->_socket), 5)) {
case 2: $this->_logger->info('Failed to connect to '.$serverHostIP.':'.$serverPort.': Connection refused');
$this->_setError(socketError,'socket_connect()',socket_strerror(socket_last_error()));
$this->closeConnection();
return;
case 1: break;
case 0: $this->_logger->info('Failed to connect to '.$serverHostIP.':'.$serverPort.': Timeout');
$this->_setError(socketError,'socket_connect()',socket_strerror(socket_last_error()));
$this->closeConnection();
return;
}
$this->_socketConnected = True;
$this->_logger->info('Connected to '.$serverHostIP.':'.$serverPort);
// Welcome Message
$this->waitForResponse();
// Connection Details
$this->waitForResponse();
if (!is_null($connectionName)) {
$this->sendMessage(':name '.$connectionName);
// Name Change Response Message
$this->waitForResponse();
}
return;
} // function constructor
/**
* @access destructor
* @return void
*/
function closeConnection()
{
if ($this->_socketConnected) {
$this->sendMessage(':quit');
$this->_socketConnected = False;
sleep(1);
}
if (!is_null($this->_socket)) {
@socket_close($this->_socket);
$this->_socket = Null;
}
} // function closeConnection()
/**
* @access destructor
* @return void
*/
function _destructor()
{
$this->closeConnection();
} // function destructor
} /* end of class IPC */
Note that this isn't complete working code, purely because of the size, but if you're a competent sockets coder, then the principles are straightforward enough
Can I recommend that you change the names of your scripts though: firstpage.php and secondpage.php suggest that this is simple web pages rather than a daemon (you are running firstpage.php as a daemon aren't you, else it'll terminate whenever you log off from the server)
Re: acess properties of an already instantiated class
Thank you Mark for your input.
I am already using sockets to communicate with an external server. (Infact that is the reason why i have an inifinitely running script. For certain specific reason i cannot close the conenction and hence the infinitely running script situation.)
I think using a database as a storage for firstpage.php and "picking it up" in secondpage.php is what i will do.
Really appreciate your taking time to help me out.
I am already using sockets to communicate with an external server. (Infact that is the reason why i have an inifinitely running script. For certain specific reason i cannot close the conenction and hence the infinitely running script situation.)
I think using a database as a storage for firstpage.php and "picking it up" in secondpage.php is what i will do.
Really appreciate your taking time to help me out.