Tag: PHP

You used to be able to run test on multiple browsers with PHPUnit Selenium test class by specifying a $browsers property. You can still do the same with PHPUnit’s Selenium 2 class made for web driver. Here is how you do it:

PHP has array_walk() which can be used to traverse an array and modify it in place. I wanted to find out if array_walk() would be faster than a loop written in PHP (for, foreach). I mean, PHP’s code is written in C/C++ and traversing anything has to be faster in C than in PHP.

So, I wrote a script to traverse an array of about 100,000 elements. Below are the results (formatted for better readability):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

new-host-2:exp moz$php for.php

Method Time taken totraverse100,000elements

========================================================

for0.030004024505615

foreach0.0066280364990234

array_walk0.019280910491943

new-host-2:exp moz$php for.php

Method Time taken totraverse100,000elements

========================================================

for0.041378974914551

foreach0.0075538158416748

array_walk0.02002215385437

new-host-2:exp moz$php for.php

Method Time taken totraverse100,000elements

========================================================

for0.029618978500366

foreach0.0064899921417236

array_walk0.018718957901001

new-host-2:exp moz$php for.php

Method Time taken totraverse100,000elements

========================================================

for0.041105985641479

foreach0.007500171661377

array_walk0.020205974578857

foreach traverses an associative array faster than array_walk() and for loop. In fact, it was much faster than both. I had expected array_walk to be faster but I was wrong. You will notice that the while loop isnt in there. I figured that a while loop would take about the same time as a for loop. Here is the script I ran for my experiment:

Before I begin, let me just say that I have tested this in PHP 5.2. I don’t know if this will work for PHP 5.3. If anyone can confirm that it does, it will be great.

You can do it with parent::someFunctionName(). You can do this from within an overloaded function also. It was confusing for me at first, so I decided to test it out myself. Below is some same code that will help you understand it too.

Sabre is a company that has data of all flight schedules, etc. And, using their system, you can book flights, hotels, rent cars, etc.

Getting PHP to talk with Sabre

I tried PHP Soap and nuSoap. They both didn’t work for me because of their limitations. So I ended up writing a small script of my own that talks with Sabre for me after I realized that the headers being sent by all the SOAP libraries I tried, were not compatible with Sabre’s web services.

Sabre checks the “Content-Type” header in your request. If that is not “text/xml”, you will always get an error, even if your xml is correct.

Here’s a little piece of code that should help with the transport part. I will leave the generation of XML to you.

Design Patterns (and anti-patterns) are techniques of programming. There are a lot of design patterns. It is upto the programmer to pick and choose which patterns to use based on their needs. Singleton is also a design pattern. A singleton is a type of class which can have only 1 object of its kind through the lifecycle of an application. It is very suitable for database, caching, etc. I will use a database class as an example as it is probably the most-widely used class in web development.

Why use a singleton you ask? A popular practice among PHP4’s developers is to make a database object global then use that variable anything a query needs to be run. It works in small applications and even in not-so-big applications. In enterprise applications where you use hundreds (if not thousands) of variables, there is a very good chance that you database object will get mutated or overridden. You also don’t want to have multiple instance of a database class (unless you are running a multi-threaded application).

A database class should be able to connect to a database, run queries, return results and close the connection to the database when it’s done. Keeping that in mind, let’s start writing our database class.

So, we wrote a class called database and made its constructor private so you can’t instantiate it using the “new” keyword. We also wrote a method/function for this class called getInstance() which will return a database object. getInstance() has a local static variable called $obj. It checks if this variable is null. If it’s null then that means there is no object of the type “Database”. So, it creates an object and assigns it to $obj. It also returns the object to the code that called it. Setting permissions of private, protected and public could not have been done in PHP 4, which frustrated a lot of OOP enthusiasts. PHP 5, however, has the capability of doing it and thus fulfilling one of OOP’s main goals – encapsulation.

The first time an object of the class Database is instantiated, it will have to pass all the variables required to make a database connection. Its subsequent calls need not do that.

Both $db and $database point to the same object. Any changes made to $database will be reflected in $db also. This eliminates the use of global variables for quite a few things (especially our database class).