PHP 5 questions



  • I've had to start working in PHP again, and it looks like 5 does do some decent stuff.

    My main thoughts though, are
      1) can you actually have class definitions in memory inbetween page calls, or are they loaded every darn time a php file is called?
      2) can class instances remain persistant in a memory space inbetween requests, selectively?

    Usually, I try to break down a web app into various levels of memory persistance, a small footprint of stuff needed for any page to run that can load fast, then request level scope stuff, and finally DB persistant items. 

    Has PHP caught up to this sort of thing, or is it still just a series of 'includes' that load everything every time a *.php file is called?

    I've only ever really 'fixed stuff' in existing PHP code and never had to worry about building an actual application framework, and I still don't have to do that, I am just curious if anyone knows how it deals with those various scopes of context.



  • duh, and of course, session level stuff, with temporary persistance, but you get the idea. 



  • @BeenThere said:

      1) can you actually have class definitions in memory inbetween page calls, or are they loaded every darn time a php file is called?

    APC does this and it will be included by default in PHP 6.  Normally, PHP has to parse each included file on every request, but APC allows the pre-compiled opcodes to be cached which will significantly reduce load on the server.  On Linux (and probably Windows) it won't really make a difference in disk I/O since any frequently-accessed file will be stored in disk cache so it's a memory read anyway, but it will reduce the overhead of the PHP parser which can give significant performance boosts on high-traffic sites.  Obviously, this requires the ability to install PHP extensions on your webserver.  Zend also has a similar product you can purchase, but it requires installation as well.  I don't know if APC or Zend Accelerator work with Windows.

     

    @BeenThere said:

      2) can class instances remain persistant in a memory space inbetween requests, selectively?

    APC can actually do this, allowing you to register "persistent variables".  This is useful for things like sessions and user objects.  However, I would not recommend this route.  I would instead suggest setting up memcached and using that.  Both operate similarly, but memcached can be accessed by multiple servers whereas the APC variables will be local to the web server.  If your app needs to grow it will eventually need multiple web servers and then the APC solution is no longer feasible because there is no synchronization between the APC instances.  With memcached the objects can be accessed by multiple clients and there are clients written in every conceivable language.  You can have multiple memcached servers as well, which will help scale the site as it grows.  You don't need a dedicated memcached server, you can just install the daemon on your web server and access it locally if you are starting with a single machine.  Even if you don't think you'll ever need more than 1 web server, it can still be beneficial to do this because you never know who will be maintaining your app in a few years.

     

    What's more memcached is a lot easier to "wipe" if you suspect data in the cache is out-of-date.  With APC you will most likely have to restart Apache on the server but with memcached you can issue a command that will flush the cache.  No matter which you use, keep in mind that memory cache should only be to reduce read load.  All data stored in memory should be stored in the database as well.  It's also a good idea to implement memcached selectively instead of across-the-board.  For certain objects like the session data, user data and other frequently-accessed data it makes sense but if you store everything in memcached you may actually end up slowing the whole thing down as every change has to be written to memcache and the database.  Memcached includes a lot of stats that you can use to profile your ratio of reads to writes.  Generally you want a large number of reads and a small number of writes because that means you are getting the most out of it.

     

    I hope that answered your questions.  I'd be happy to help out if you have any further questions. 


Log in to reply