Categories
PHP

The Orchid Experience

For our next project at Leevio, it has been decided that we will use Orchid as our PHP framework. It’s a framework created by Hasin Hayder, the founder of Leevio and the PHP Guru of Bangladesh. Today I was assigned to make myself used to this framework. I don’t have any prior experience of working with a fully fledged frameworks. So I was a bit worried. I checked out the Orchid framework codes from the Google Code SVN repo and installed the framework in my localhost. First I failed to make it work because by mistake I have left the .htaccess file which was hidden in my file explorer by default. Soon, I understood that I have left behind the lifeblood of a framework — the URL rewriting definition in the .htaccess file. I quickly pressed Ctrl + H to unhide the hidden files and copied the .htaccess file into my www/orchid directory. Whoa! I was now able to see the welcome page at http://localhost/orchid/. While I must admit that Hasin vai has a worse sense of what a welcome page should look like, on the other hand I must admit that the messy page took much of my tension away 😉 😛

I examined the sample apps for a while but since I have no prior experience in a whole framework like Orchid, I was having a tough time understanding how the wheel rotates inside Orchid. But I could ocassionaly make some educated guesses and my asking Hasin vai proved that all of them were correct 🙂 I was now more comfortable at exploring this new Island that at first seemed to be so full of vicious creatures.

After a while, I realized that I am that sort of stupid who can’t understand theories until he has done some practical work. I decided to do something on my own. While browsing the source, I found a “cli.php” in the root directory of Orchid. Those who know me or have read enough of blogs, they’d know that I am badly fascinated by cli tools. Examining the cli.php file, I could figure out that this one creates skeleton codes for an app. The cli tool has two options — create a skeleton app or an app with pre-installed controller and views. I chose the second option.

First, I removed the samples those came bundled up with Orchid. Then I opened up my terminal and fired off typind the following codes:

That’s all I needed. First one changed the current working directory to the Orchid directory. The second one invoked the Orchid CLI tool to create a controller for “masnun”. Again, I should criticize Hasin Vai, the CLI tool prints nothing but a single line saying — “Welcome to the CLI tool of Orchid” or something like that. Though it gives an error message on failure, I don’t think thats any credit of the tool in question, rather the error message is generated by PHP itself. On the other hand, on success, you get not even a single line congratulating you or at least saying “hi”. I am very much offended by such rudeness 🙁

Anyway, I was now ready to view my app at: http://localhost/orchid/masnun/

It showed a simple text saying “Called from masnun/base method”. That made sense to me. There is a “base” method inside the “masnun” controller. I opened up the controller file and yeah, there really was a base method and another method named “hello” 🙂 I was curious to see, what this other method does. So, I visited out of curiosity the following URL:

http://localhost/orchid/masnun/hello/

The result was somehow obvious, yeah it printed “hello world!”.

Now I came to think about it. How does Orchid work ? Well, I was feeling stupid now, since it has now been quite clear. The URL format is:

/controller/method.

If you just visit the controller, the base method is called. That is:

“/controller/” == “/controller/base/”

It was easy as pie! Then I moved on to using templating. Opened up the view directory and found two files — “base.php” and “hello.php”. I had no doubt that they had markup for each of the method of the controller. I opened up the controller again to examine.

Aha… I don’t even have to explicitly define the view ? Cool… Hasin vai really has some talent then 😉 Every method auto loads the curresponding view 🙂 That cuts down the work of a programmer for sure. But wait! How can Hasin vai be this stupid ? What if I need to setup a different view ? Oh my god! I have to tell him about this.

I was frustrated with Orchid and closed the current working copies and started examining the sample apps. At that time, I realized that Hasin vai was not stupid at all, he’s a genius ! He does have the “setView()” method. I was relieved and feeling sorry for cursing the gentleman for a while. But still, I would defend myself because if Hasin vai had a good documentation, I would have known it long before and didn’t need to curse him at all. So, you see? It’s his fault after all and he does deserve some curses for not having a total documentation. What do you say ? 😉

I immediately used the following code in the hello method:

Wow ! It does work. I have been able to reproduce the base method view just by changing the view of the hello method. I am damn happy.

Later I found out that, there’s a “layouts” folder inside the “views” directory where I can define the global layout for a controller. I created a “masnun.php” in the layouts directory and put these code:

Cool ! The template was applied to the whole controller — that is to both the base and hello method.

I am just loving this framework! Orchid has some cool built in libraries for additional features. Have a look at it’s core features:

1. Support for Caching
a. SQLite Based Caching
b. MySQL Caching
c. Memcached Caching

2. Built in Unit Testing Support
3. Fast and Lightweight
4. Support for MySQL, MySQLi, SQLite, MSSQL and PgSQL using Native DAL and PDO
5. Support for Layout
6. Active Record
7. Easy Application Setup
8. Flexible Configuration Management
9. Builtin Profiling
10. I18N support
11. Session Manager (Native and DB Based)
12. Excellent support for benchmarking and profiling
13. Builtin JSON Encoder and Decoder
14. Google Chart API Support
15. Bundled JS Libraries with Gzip Support
a. Prototype
b. JQuery
c. SWFObject
16. Builtin Library for jQuery Effects and AJAX Support

PS: I am still learning Orchid and hope to keep posted about my progress. I am now concentrating on understanding the models system of Orchid 🙂 A big thanks to Hasin vai for this wondeful framework !

Categories
PHP

PHP Multi Threading :)

Though not available under apache for processing web pages, we do have multi-threading in PHP 🙂

The “pcntl” extension enables multi threading in PHP command line interpreter. That is you can work with php multi threading only from command line. Here’s a code snippet:

The above example first calls the pcntl_fork() function that creates another process with the same data. That is the current execution data is transferred into a new process. Both the processes will advance in the same way. We will have to differentiate the two processes from this point and assign two different tasks to them.

We differentiate the processes by using the return value of the pcntl_fork() function. If the function is successful in creating a new process, it will return two values — one for each of the processes. And a single value if failed. As you can imagine, if it fails, we will have that single process which started at the beginning. On success, we have two processes running at the same time. Both executes the same php script. But the return value of the pcntl_fork() function varies. So, we should add some code to the script that determines the process which is executing the script and act likewise.

The return value of the function could be of three types:
— A Process ID
— 0 (Zero)
— (-1) (Negative One)

If the process that’s executing the script is the child process, it gets the return value 0. And the parent process gets the process ID of the child. (-1) means the forking failed.

On the above example, we have used posix_getpid() and posix_getppid() functions to retrieve the process ID of that process and it’s parent’s.

The process of forking is a bit tough and it took an hour of total wilderness to understand how it really worked. And to be honest, the PHP manual is not that helpful regarding this extension if I compare to other PHP functionalities.

Categories
PHP Python

Creating Dictonaries from Arrays / Lists

PHP:

In PHP, we can create a new associative array or dictionary by using the array_combine() function. Just feed two arrays as the parameters to this function and you get back a dictionary. The first array elements are converted into keys and the second array elements are used as values.

Python:

For Python, we first have to zip() two lists into another lists of tuples containing one element from both lists. Then we use the dict() call on this newly created list to create a dictionary.

I elaborated the process in the above example for beginners. But you’d often see advanced python programmers do it like this: