Page 1 of 1

Emulating ASP's Application object with php

Posted: Mon Sep 02, 2002 1:28 pm
by samscripts
Hi, just wondering if anyone had tried to emulate the only thing i like about asp - the application object thing that lets you set a variable that can be accessed from all scripts?

I'm trying it myself using a little class that reads itself from a specific file at the beginning of the script and then serialises itself to the file on script termination, and was wondering if anyone else had tried anything similar they wanted to share?

For anyone interested, the basic usage is like this (more or less)

Code: Select all

<?php

/* include the application class - not shown here because it is currently somewhere in between concept and code  */

include("application.class.php");

$application = new application();

if( !isset($application->counter ) ){
   $application->counter = 1;
}else{
   $application->counter++;
}

echo "This server has served ".$application->counter." requests for scripts using application object";

?>
this example would just count how many times pages were requested - useless i know but meant as an illustration of how to use the class.

If anyone has tried something similar, I'd love to hear about it.

cheers, Sam

Posted: Mon Sep 02, 2002 10:24 pm
by phpPete
Sorry...spoke to soon...found this article you might find interesting:
http://www.tapinternet.com/index.php/ne ... event=view

Posted: Mon Sep 02, 2002 10:28 pm
by volka
the application-object is not bound to a session. Every script of the same application (server) can share the data stored in .application
edit: hmm..this post became useless ;)

tap internet article

Posted: Mon Sep 02, 2002 10:43 pm
by mgkimsal
I wrote that piece - I'd suggest following it's model of writing things to a database instead of serializing to a file. Let the database handle concurrency issues with data storage - don't try to solve it in your script if you can avoid it. Let me know if you have any questions. :)