Aiguo,
To achieve good performance, I want to load the
whole dictionary into memory when the "application" starts (e.g.,
when
the web server starts...
I'm not sure how ASP works, but in PHP you can't load something into
memory when the web server starts and keep it there. PHP 'compiles'
each script upon request and then frees up the memory it occupied.
I'd think that this would be counter productive if your looking to
achieve good performance because you'll be re-building the dictionary
on every page load.
It might be better to let another application load the data into
memory and hold it there instead of making PHP load the whole thing
every time a user requests the page/data. If it were me I think I'd
simply load the dictionary into a database and build a simple library
or class to perform queries and calculations against the dictionary.
Assuming your class definition takes care of connecting to the
database server you could lookup a word in almost the exact way you
demonstrated.
e.g.
$dict = new dictionary;
$definition = $dict->lookup($word);
If you'll need this object to be available on every page the user
sees you could always make a 'common.php' (or whatever) which would
create the object and then include that at the top of each page.
For a complete solution, it should support object locking/exclusive
access as well.
Maybe I've been a spoiled PHP programmer for too long. ;-) I'm not
sure what object locking/ exclusive access is but from the sounds of
it, there isn't a way to do this in PHP that I'm aware of. This is
assuming that you want to be able to basically remove the object from
the global scope. In PHP objects are globally accessible from the
moment they're created to the moment they're destroyed.
Good luck!
-Joe
On Jul 27, 2005, at 8:40 PM, Aiguo Fei wrote:
In ASP.Net there is the concept of "application" which is a virtual
directory on a Web server. An application can have application-wide
shared data/objects, which can be accessed by any script of that
application (i.e., scripts under the virtual directory). I have gone
through several PHP books, haven't seen it mentioned anywhere that PHP
has similar facility. I don't know if anyone has written an extention
to do something similar. Or any experience/suggestion on this matter.
To make it clear, consider the following example. I want to do
web-based dictionary. To achieve good performance, I want to load the
whole dictionary into memory when the "application" starts (e.g., when
the web server starts, or triggered when the first script under a
certain directory is requested), and build a lookup table; then create
a globally-accessible object, let's say, Application["my_dict"]; and
it provides a function to do the dictionary lookup. In a script, one
could do something like:
$definition=Application["my_dict"]->lookup( $word );
For a complete solution, it should support object locking/exclusive
access as well.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php