Page 1 of 1
PHP Help for J2EE guy
Posted: Sat Dec 27, 2008 8:44 pm
by rpaturi
Guys,
I want to thank in advance for your help. I am J2EE guys and moving to PHP, for obvious reasons of lack of reasonable hosting support. (PHP has great support ex: mosso.com etc cloud support). The app I am writing will have at least guaranteed 3 million total users, 20,000 concurrant users (logged in) and estimating about 500 simulatenous requests.
I am thinking in Java but coding in Php (please consider my ignore when helping please). I googled these enough but could not find definitive answer.
1: I am used to storing some reference data in memory globally for quick access (example: list of countries, list of languages etc). My understanding is that there is no persisitant memory saving in PHP. The only reference I found is to store these in flat files, read the file and make an array to make options. Is there a better way ? Is this approach faster than connecting to MySQL. (In Java I found the difference is over 100 times between memory vs mysql pool connection)
2: I am planning on implementing multilingual support, i.e. all labels in the site needs to change to specified language. In Java / I store the strings in MySQL, as <Original String>,<lang><UTF lang string>, I loaded the entire table into memory, (roughly 100K). I pass the original string to an object, if the language is english it returns back the same string, if the language is arabic, it get;s the arabic string from memory. This is fast, as i dont have to mysql for each string. Can you please help me how can I implement this in PHP. I can store these in file and read in to buffer, but I have to read the cache file for each php page.
3: I typically found the difference between using connect on demad vs using database pool is about 1000 times. I could not find a good way to open x number of pool connections to my master/slave databases.
4: Is there a limit on data size I can set for a user ? I should know this but I don't can I attach arrays to session ?
Thanks you in advance, any code snippets will be appreciated to max.
- Ravi
Re: PHP Help for J2EE guy
Posted: Sat Dec 27, 2008 10:17 pm
by Christopher
In general you just don't worry about loaded everything every request in PHP. That you will have to get use to that major difference. If you are really writing an app for as many users you may want to look in to memcache. There is some information about connection pooling and persistent connection in PHP around the net as well. And yes you can store arrays, and even objects, in the session.
Re: PHP Help for J2EE guy
Posted: Sat Dec 27, 2008 10:40 pm
by Chris Corbyn
I agree that memcache or xcache could solve some issues here. I'd possibly consider Amazon EC2 as a hosting enviroment. They have some damn good offerings and there are tools out there for vertical scaling in EC2 clouds at the push of a button when you need to do so (EC2 is a pay-as-you-use service so you can bring up and shutdown instances in the cloud according to demand).
Shifting from Java to PHP at an enterprise level may take some significant changes to your approach to certain problems. PHP cannot multi-thread for one. We heavily depend on things like cron scripts (there are other approaches however) to achieve asynchronous tasks. You've also discovered some of the hurdles when it comes to global persistence.
If you're shifting from J2EE specifically, you'll probably be completely lost with a base PHP installation. I'd suggest researching the available frameworks sooner rather than later. PHP has a fair selection - some of them a little influenced by RoR.
I guess you've already figured this out, but in terms of hosting you want PHP 5.2 at least. All of your thinking of Classes, Abstract Classes and Interfaces won't go too far without it

Unfortunately not anonymous/inner class support in PHP which can occassionaly lead to bloat (IMHO).
Regarding your #2. PHP in it's current (production) state isn't very good at dealing with multibyte character sets. Terrible in fact. PHP 6 has been in development for well over a year now (someone remind me?) and adds full unicode support so we're getting there.
#3. You can use connection pools in PHP. I believe Doctrine will help with this. Again, I'd suggest finding a decent ORM library since I'm guessing you'll be pretty dependent on Hibernate? Doctrine is held with pretty high regard.
I think ~arborint covered your #4 really. PHP does have a memory limit, though it's a configuration settings so if you're managing the installation then it's not likely to be a problem. One of the projects at work uses mecache for session handling so that a DB write doesn't occur on every page. We run on clouds (Amazon EC2 incidentally) so the standard session support isn't an option since it writes to disk.
Re: PHP Help for J2EE guy
Posted: Sat Dec 27, 2008 11:53 pm
by rpaturi
Thank you for suggestions. I just though of couple of approaches please suggest if these can even be done.
Write / create a class.php dynamically and include that class file in each page php that I want to use the global objects ? The class.php file will be created by another initClass.php that accesses database and makes the class.php. class.php will be $array['var1']="db result"; etc.. I will call the initClass.php when ever I need to reinitialize the array if any core data changes ?
--
Can include a php code from one file in another (not after execution). I want to include the code from 1.php into 2.php and execute 2.php as a whole that includes the code from 1.php (not result of executing 1.php) and the interpreter should treat includes as part of code of 2.php. (I do not know how to do this, and you guessed right I may generate 1.php dynamically from another php file)
Re: PHP Help for J2EE guy
Posted: Sun Dec 28, 2008 12:19 am
by Chris Corbyn
Generating PHP files dynamically sounds scary, expensive, and dangerous. Can you elaborate on why you need to do this? There has to be a better way.
Includes in PHP work like includes in C. The source code itself is loaded and read when you include the file. The "result" of the code (i.e. HTML) isn't what you get no, you have access to all classes, functions and variables created in the included file.
Re: PHP Help for J2EE guy
Posted: Sun Dec 28, 2008 12:34 am
by rpaturi
I want to load all site's l10n strings from db and create a class file dynamically. That class will have code to create an array in the class. I will include the class file php pages to access l10n labels to change the text from english to other languages, when user selects to see the site in another languages (similar to linked-in or facebook style). hopefully I will test enough of the db->class generation code to prevent any errors.
And I guess am trying to invent ways to use my J2EE designs in PHP and trying to find a way to create global persistance for data that does not change much during the life of instance.
Can that be doable ? (PS: Thank you on the include, I experimented and looks good)
Re: PHP Help for J2EE guy
Posted: Sun Dec 28, 2008 1:32 am
by josh
I'm sure its doable, but I thought you were trying to write an app 3 million users were going to use, not try to invent ways to undermine problems computer science already solved. You still haven't articulated why this architecture is needed. If you want to handle character sets that has nothing to do with code generation... nothing at all. If you want character set support in the source code itself it'd be worth compiling a PHP6 snapshot, too.
Re: PHP Help for J2EE guy
Posted: Sun Dec 28, 2008 8:39 am
by rpaturi
You are right. We have to wait for PHP6 regardless (and have the rest of code ready).
I could be wrong but when I was researching no one offered memchace at standard cloud hosting environment (mosso style). (In Grid/Slice yes I can get memcache, as we can control the app env on the instances ex: amazon ec2, gogrid).At present we are leaning towards ec2/gogrid so will get memcache.
The reason I am looking for global memory instance for speed and saving memory.
One example is as I mentioned, l10n. Each label on the site needs to change to user config/choosen language.
1: I do not want to go to DB for translation of each string.
2: I did not want to attach the l10n data to individual user session as I have few thousand concurrant users and I will be duplicating same data (about 150K) for each user.
3: And the translation of strings is being contributed by users themselves - i.e need to change once in a while.
Other examples.. typically in design, we save several reference variables. we use these variables intensively almost few times a page per user per session. For example country is always coded with iso2 in DB (us, es, in etc..) but we have to display full form on screen. (United States, Spain, India etc). These are preloaded in memory we use a iso2name function that translates the us,es strings to full name on display. Being in memory is extreemly fast - same as if we had static country name.
Re: PHP Help for J2EE guy
Posted: Sun Dec 28, 2008 1:50 pm
by Chris Corbyn
Can't you just keep all of your l10n strings in subdirectories and dynamically load the correct one? This is what polymorphism was supposed to achieve. I can't see a reason for generating the source code at runtime.
Re: PHP Help for J2EE guy
Posted: Sun Dec 28, 2008 3:30 pm
by josh
You cache it and you make the update code delete the cache file so the next thread gets it from the database, if a thread has to go to database it's responsibility should be to re-create the appropriate cache file for the next thread. I think what you mean is you want to generate the code to load the settings into a stub object, rather then parse a configuration file... that's entirely different from what I thought you were suggesting.