Debugging PHP app using xDebug and Eclipse Tutorial

This will be a tutorial on how to debug a PHP application using xDebug and eclipse.

1. Get the eclipse for php from Eclipse for php
A side note, if you are on any *nix OS, this might require you to update your java to 1.7 and above. If that is the case, download latest Java JDK from Oracle (1.8 at the time of writing) and do:

which java

in most cases, this will show /usr/bin/java which is a symlink to the actual java binary.
Follow the symlink using

ls -la 

That depending on the destro you have, would point to the link destination, there you can point to the java symlink to

ln -s /path/to/the/downloaded/jdk/bin/java

That should take care of the problem.
On windows, you need to updateh environment variable that you set for the JAVA_HOME. Googling on this shall give you the right direction.

2. Get xDebug on your system
Well, before this you might need the whole stack of apache, or any other php-approved webserver, and php itself. For that if you are on *nix you can use

sudo apt-get install lamp-server^

On windows, you can use wamp server and be done with it.

To install xdebug on *nix

sudo apt-get install php5-xdebug

Once you do that, restart your apache server

service apache2 restart

and do phpinfo(); you should be able to see the xdebug info there.

Now, pull your php.ini and add the following to it

[xdebug]
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

You can alternately add this info in the xdebug.ini if it it exists in the additional ini folder. This information would be available on the phpinfo();

Again restart your apache web server.

For testing purposes I have created a simple yet good php example on https://github.com/gullele/simple-php-calculator-example

Go to your eclipse Run>Run Configurations..
and do the following on respective tabs
For server tab:

server
For Debugger tab.

debugger

Now your eclipse is ready for debugging!
You can put a break point by double clicking on the area you would like the debugger to stop.
As you can see, I was able to see the value assigned on the $config variable that is was read from config.ini file.

debugprogress

, , , , , ,

Leave a comment

Seasoned PHP programmer toolkits

PHP has grown a lot in the past decades and is becoming one of the emerging programming languages. There are still problems with it of course, www.phpsadness.com, but I would say some of those problems can be easily avoided using a better programming standards.
I have compiled some basic tools/methodologies that we should at least know them to be a good PHP-abiding citizen. Some of the points are applicable to being a good programmer in general.

Version Control Systems
It is almost a must to know one basic versioning system. I would say it is good to have a knowledge of distributed versioning system. Here is a discussion regarding the central versus distributed versioning systems Difference between version systems
From the DVCS I have an experience on mercurial and git and I would say git is better.
Why using VCS? They become handy wheather we are working in team or solo programmer. Some examples:
-when the release becomes really creazy and we want to go back to the earlier solid version(checking out older revisions)
-To save the current working progress and be able to look on work another revision (stashing)
-To be able to search revisions based on comments of the commits we have
The above are some of the most commonest things we use day today without mentioning being able to work from different computers and other minor routines.

Dependency Manager
Composer has become a common inclusion on most of the frameworks for managing the depencies on the progect.
You know when you are working on a project it would be dependent on a lot of libraries. Some of them from third parties, some from github, some from PEAR… well how are we going to manage all these dependencies with out a headache? Composer!
Using composer.json file, you will be able to list all the dependencies you need for the project and it will cache it locally. So next time when you require the same dependencies it will be really fast to load it since it is loading from local computer not from remore githut repo or something.
What is the use apart from this? We will have a light weight application composed of the main business logic. All the third party stuffs will be loaded whenever they are needed using composer install command.
Other advantage is it will lock the dependencies using composer.lock file. By doing that we are assured that everytime when we are installing, it will using the lock file to fetch the dependencies.
You can get a lot of goodies that are prepared for composer from https://packagist.org/

Coding Standard
It is a vital part in coding in general – to have some coding standard. These days, lots of projects and even composer might require you to follow basic coding standards and for php, the defacto is ps-r one.
http://www.php-fig.org/psr/psr-0/
http://www.php-fig.org/psr/psr-1/
http://www.php-fig.org/psr/psr-2/
http://www.php-fig.org/psr/psr-3/
http://www.php-fig.org/psr/psr-4/

Caching
Caching is almost a must on most of high traffic sites. The concept is simple: it is faster to read the data from memory than from database/file. We do encounter a lot of data that do not change often like configs and hence it would make sense to put them on memory than reading them from database.
For this we can use Memcache/d http://memcached.org/
and Redis http://redis.io/
Both have driver for php and are widely used.
They allow object persistence through serialization so all the cost of reading from the database and constructing the object could be saved by tossing end-product object into the memory and use it.
*If you are working on Doctrine, it might not work well with those outside of its own caching mechanism. But that wont be the problem – just use its own!

Vagrant
I would say it is one of the best development tools. It is based on the concept of virtual machine. Can you imagine the problem you would face to setup a new machine for development, installing drivers for some of the packages, installing the language dependencies… with vagrant you would do all this only once. And you can use that same configuration again and gain. All you need to keep is some simple files containing your configuration.
Learn more on http://www.vagrantup.com/

IDE
This is totally personal. Some of us would be happy and productive by coding from vi or notepad others from eclipse.. the list can go on and on.
I have been on Eclipse, netbeans, vi, sublime..
By far I would say phpstorm is the best. http://www.jetbrains.com/phpstorm/ It would come with a month of free trial which will give you the chance to play with it.

No SQL Databases
We are all familiar with relational databases like mysql. With no-sql ones, the story is totally different. The are document based storage systems and they scale better horizontally.
To see it with and exmple, in a typical employee, department, company situation, we need three tables, assuming they are normilized in all forms, so to get the information of employeeA, there would be two joins involved. But in a no-sql database the data might look like, not exacly but, array (‘employeeAId’=>array(‘company’=>companyInfoGoesHere, ‘department’=>DepartmentInfoGoesHere’));
You can see that selecting the database with single employeeId would give us an access to all the related data. Hence it scales horizontall.
As you can see, it might not be a solution for all cases. But it will definitely boost our performance with our old school relational database.
Take a look at http://www.mongodb.org/

I will write on
Debugging
Testing
Reviews
Workflows among others on my next blog.

Leave a comment

Multiple keyword search in multiple directories bash script

I guess the title is good enough :)
Probably you can do it in single grep command too.. but this way you will play with bash as well..

here is the bash script doing it

 #! /bin/bash
 dirs=(dir1 dir2 dir3 dir4 dir5)
 keywords=(keyword1 keyword2 keyword3 keyword4 keyword5)
 strs=""
 for word in "${keywords[@]}"; do
     strs+=" -e ${word} "
 done
 
 for path in "${dirs[@]}"; do
     dirname="$(basename "${path}")"
     grep -ir --color ${strs} ${path}
 done

Save this as finder.sh
and you can run it as bash finder.sh

, , , ,

Leave a comment

Connecting to Vagrant [PuPHPet] MySql from host

Probably here with – I can’t connect to mysql on vagrant machine or something… yup I have been there
If you are php developer and are using vagrant you would want to use mysql tool to access the vagrant mysql server.
Here is the step by step procedure.
I will assume the PuPHPet configuration
First, get the user which can connect from any host
Log into your vagrant

vagrant ssh

Then open the mysql configuration file

vi /etc/mysql/my.cnf

And update the bind-address part to be 0.0.0.0 this will allow address to be used from any host. If you want to specifically access from some host only, then put IP over there.

Then being in your vagrant machine run

mysql -uroot -p123

This will be depending on if you have provided a password for root the default on puPHPet would ask password so you would have a password
After this allow root user to access everything from everywhere.. or you can create another user and grant that

mysql > GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123';
mysql > FLUSH PRIVILEGES;

Then to make the sql pickup the changes made so far

sudo service mysql restart

Then being on your host machine open your sequel pro – if you are on mac.. or other clients you would use for mysql.
Then
Mysql Host: 127.0.0.1
Mysql user: root
Mysql Password: 123

SSH Host: 127.0.0.1
SSH Username: vagrant – this is the default one
SSH key -> point the key in puphpet/files/dot/ssh/id_rsa
SSH Port: 2222 – this is also the default ssh port forwarding, if you change this change it here too..

This should be it..

, , , , , ,

Leave a comment

Vagrant running slow sites

Using vagrant and running a website from it looks so slow when you access it form host?
Then you are not alone.
Just enable NFS and you would see an increase on performance.

If you are using puphpet, on your config.yaml file under the synced_folder, set nfs to ‘true’. Otherwise on your Vagrantfile, look for nfs and set that to true.

, , , , , ,

Leave a comment

Guest machine entered an invalid state Vagrant error

I used to use vagrant only at work place. But yesterday I upgraded my mountain lion to maverics and my php | ruby setting was jacked up.
I decided to use vagrant at my home as well and got this error.
It appears that I had an older version of Virtualbox and upgrading that helped the problem in no time.

Happy Vagranting

, , , , ,

Leave a comment

soft rlimits too low error on mongo

This is typical memory issue. Are you running a bunch of heavy programs along side? Check how much pages you are loading on chrome even.. and your IDE..

There is a way to add more before you start mongod though

ulimit -n 2000 && mongod

But I am a fan of using the resource effectively by removing apps that takes up my memory..

, , ,

Leave a comment

Follow

Get every new post delivered to your Inbox.