Archive for the 'PHP @en' Category

Install PHPUnit on WAMP

Carl on jun 10th 2012

Before you can install PHPUnit you need to have installed PEAR. If that is not the case , you can do the following:

  • Go to wamp-directory, b.v. :
    C:\>cd wamp\bin\php\php5.3.0
  • To install PEAR, execute the following command:
    You will be asked several questions which you can leave to the default value.
  • The next thing you will need to do is register PEAR environmental variables in the registry. Therefore you can execute C:\wamp\bin\php\php5.3.0>PEAR_ENV.reg (Perhaps not a bad idea to backup your current registry!). Now you can call PEAR everywhere on the command line
  • If you cannot call PEAR on the command line, you will need to add the PEAR directory to the Windows environmental ‘Path’ variable. You can find that one in: System Properties -> Advanced-> Environment Variables. Select the ‘Path’ variable, click on ‘Edit’ , and add to the end of the line a ‘;’ and the path to the PEAR directory which in my case is: C:\wamp\bin\php\php5.3.0

Install PHPUnit

  • Register the PHPUnit channel in pear C:\wamp\bin\php\php5.3.0>pear channel-discover
  • Install PHPUnit C:\wamp\bin\php\php5.3.0>pear install phpunit/PHPUnit
    of php5.3.0>pear install –alldeps phpunit/PHPUnit

If you get a warning your PEAR verzion is not high enough, you can upgrade your PEAR version:
...php5.3.0>pear upgrade pear

Filed in Miscellaneous @en,PHP @en | Reacties uitgeschakeld

ADODB-change database

Carl on aug 9th 2011

In ADODB you cannot use multiple database connections simultaneously. If you need to work cross database, you have two possibilities:

  • Close the existing connection en setup a new one so you can retrieve the required data from the database;
  • Change database.

1. New Connection

$dbhandle = new ADONewConnection( $DSN_1 );
... queries etc. ...
$dbhandle->close ();
$dbhandle = new ADONewConnection( $DSN_2 );
... queries etc. ...
etc, etc

2. Change Database

$dbhandle = new ADONewConnection( $DSN_1 );
... queries etc. ...
$dbhandle->SelectDB ( $databasename );
... queries etc. ...

At first sight the differences do not seem to be very big. However, if your database server has a heavy load, it will certainly make a difference. The first option initialises a new connection every time (even with pconnect) and though you closed the previous connection, the latter still needs to timeout on the server. As a database server is no different then e.g. a web server in handling connections, this means you will be running out of connections (max connections in the server configuration), thus your code will be waiting for a response as the requested connections are being queued. Result for the web application … page timeouts.
The second option re-uses the existing connection. It will only switch from one database to the next. This, however, is only possible if the databases you use can be read by the same user from the same host, as you will understand. The bottleneck of this method is your service provider: Does he, if you are not the one providing the service, allow you to select/update/delete in your multiple databases with one account.

Filed in MySQL @en,PHP @en | Reacties uitgeschakeld

Recursion (php3/4/5.0)

Carl on mrt 2nd 2011

Recursion should work fine in the latest versions. However, if it doesn’t read on and try another way …

PHP can be a bitch … One problem you will certainly encounter is when you want to call a function recursively. After you have been sweating on your code for half a day, a day, or maybe even longer, you will find that the initial value of your array|hash is no longer the same. It suddenly has diminished to a single value …

However, you can call a method recursively, not a function. Confusing? Yes, quite, as this means that you will only be able to use recursion in a class/object. It may seem you are writing functions in a class – you define them with ‘function’ – but it is a method your creating.

To call a method recursively, you cannot use the default way you would call a class method: ‘$this->methodname ([all your paramaters]);’. Recursion can only be established with: ‘classname::methodname([all your params);’. If you use the first method ‘$this->…’ you will experience the same frustration as with calling a normal function recursively. However, the second way to call your method – ‘class::method ..’ – puts you variable in a new instance, which does not know an previous values, and thus you can iterate recursively over your array|hash.

P.s. Watch out with static methods/functions

Filed in PHP @en | Reacties uitgeschakeld

Class Variables PHP5

Carl on feb 24th 2011

One nice quirck in PHP5:

If you write a class and use boolean variables in the class methods, you will need to initialise/declare it at the beginning of your method.

function methodenaam () {
$booleanvar = false;

If you ommit that, you will have the unexpected experience (in PHP5) that your variable will return ‘false’, even after you are 100% certain, it should be ‘true’. Anyway, it is a sign of good programming to declare/initialise your variables.

Filed in PHP @en | Reacties uitgeschakeld

Zend FrameWork

Carl on feb 2nd 2011

More and more frameworks are being written and used. Thus, as a programmer making you work quicker (and more efficient?). A starting programmer may find it difficult to get on to speed with the, sometimes, unclear workings of those frameworks – as a matter of fact I do too sometimes …

One of the many framworks currently available is that from Zend. This framework exists for quite some time and offers many possibilities. I hav never been able to find a good tutorial until recently. The one I liked you will find at including sources.

Filed in Frameworks @en,PHP @en | Reacties uitgeschakeld

Using Quotes

Carl on jan 26th 2011

Quite a few (starting) programmers use the double quote style. Often they say it is because it’s easier or better readable. However, PHP is an interpretive language and will interprete everything enclosed in double quotes. This may affect the speed negatively when using large blocks of code.

Text enclosed in single quotes will not be interpreted and shall not influence your code’s speed negatively. Having a site with high trafic and a lot of code to work through, it is best practice to use single quote style and concatenation.

echo “Welcome to {$_SERVER[''HOST'']} bla bla, $_today”;
echo ‘Welcome to ‘ . $_SERVER['HOST'] . ‘ bla bla, ‘ . $_today;

Filed in PHP @en | Reacties uitgeschakeld