Categories
PHP Python

Handling Exceptions in PHP and Python

It’s really a very important programming practice to properly handle any possible exceptions… It makes the code more stable, efficient and re-usable.

PHP:
PHP now has a very good exception handling mechanism. Just put a “try” block and then “catch ” the exception if anything goes wrong 🙂

Have a look at the following code:

The code is self explanatory. But you must use the “Exception” word before $e in the catch block to get things going perfectly. Like I did, you can “throw” a new exception in case there was really something unexpected in your code 🙂

Python:
Python is just a fantastic language. Here’s the Python equivalent of the above code snippet:

The only difference in PHP and Python’s exception handling is it’s language structure. In Python, we don’t throw an exception, rather we raise one. Just a bit of politeness! 😉

Conclusion:
Both PHP and Python has very good exception handling techniques. I can’t discuss them all on this blog but you should consult the respective manuals to master them. They are really very essential! 😀

Categories
PHP

PHPRackCloud: RackSpace PHP Client Library from Leevio

RackSpace Cloud hosting is one of the most popular Cloud hosting services today. For an upcoming product of Leevio, we decided to choose RackSpace for cloud service. RackSpace has a very decent API but quite unfortunately there was no language specific client library available for PHP. The people at Rackspace were developing one for Python but we are solely using PHP for our current project. So we chose to write a PHP Client library ourselves.

As a consequence, a PHP Client library has been released from Leevio and it has been approved by Rackspace. The library is as-expected released under the New BSD license and hosted on it’s own Google Code page.

And lately, we have developed a product page for PHPRackCloud at our website as well.

Official Website: http://leevio.com/phprackcloud

Download: http://code.google.com/p/phprackcloud/

Have a look at the Wiki Page for documentation. Most of the code is self explanatory and adequate examples have been included in the download package.

The team is very happy and excited because of the recognition from RackSpace. We are glad that we have been able to create something really valuable in the first month of our start up.

Don’t forget, Leevio is still too young… But I believe we have a really long way to go… 🙂 Wish us luck !

Categories
PHP

Using Memcached with PHP

Memcached could be a very efficient way to scale your web applications. Memcached lets you store data into the cache of your memory and quickly retrieve data when you need. So, it is usually very fast for data storage. But the darker side is that it’s extremely volatile. Your machine crashes or restarts and all the data are gone. Still, memcached is widely used as a caching device to store temporary data for decreasing the load on mysql database.

Memcached is supported by many of the scripting languages. PHP has excellent support it. They have a PECL extension for interacting with Memcached.

=====================
Setting Things Up
=====================

1) Install Memcached on your machine.
2) Install Memcached PECL extension for PHP.
3) Activate Memcache.

1) Installing Memcached:

To install memcached on your Ubuntu type the following command on your terminal and press enter:

To install on Windows:

1. Download memcache from code.jellycan.com/memcached/ [grab the ‘win32 binary’ version]
2. Install memcache as a service:
* Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] – thanks to Stephen for the heads up on the new version
* If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
* Install the service using the command: c:\memcached\memcached.exe -d install from the command line
* Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start “memcached Server”

2) Installing The PECL Extension:

On Ubuntu, use the command on terminal:

On windows, you need to get the memcached PECL extension, put it into the PHP’s extension directory. You might get the file at: http://downloads.php.net/pierre/

Add the extension to php.ini.

On Linux:

On Windows:

3) Activate:

Make sure that Memcached is running as a daemon/service. If not, use the command:

Now start using memcached 🙂

Memcached Demo: How to store an array ?

The Memcached PECL extension has a built in class “Memcache” which we will use to work with Memcached.

Pretty easy… Isn’t it?