Archive for the ‘PHP’ category

Generate PHP Docs using Netbeans

August 8th, 2011

In any project development life cycle, documentation plays a key role. The advantages for the same are quite obvious.

In a big team it plays a crucial role. The reason for the same is if a developer has spent many days in creating a piece of code which has to be reused by other team members. If the documentation for the code is done in a proper way, other team members can simply refer to it and doesn’t have to seek much help from the developer who has developed it. Hence it is the responsibility of the developer to provide good documentation for it.

In java the above is achieved using Javadoc. PHP also has come up with a command line tool called PHP Documentor on the same line of Java doc.

Netbeans has provided a plugin to PHPDocumentor in there latest release Netbeans 7.0.

Installation of PHPDocumentor and integration with Netbeans

Prerequisite

- Netbeans 7.0 should be installed

- PEAR should be installed

Install PHPDocumentor

  • Run the command PEAR install –alldeps phpdocumentor
  • The above command will install PHP Documentor along with all the dependencies

Integration with Netbeans

  • Open Netbeans
  • Go to Tools->Options->Phpdoc and verify that it is identifying the PHPDocumentor installation. Please refer to the image given below
PHP Documentor Set Up

PHP Documentor Set Up

  • Right click on the project, select Generate PhpDoc
  • Enter the target directory where you would like to store the documentation and click Ok
  • Verify the output window
  • Once documentation generation is done, default browser would open with the index page for the documentation

Thanks,

Ushainformatique Development Team

Access file in restricted folder in PHP with cURL

January 29th, 2011

Requirement

Many times, in a software project, a developer is required to access a file in a restricted folder as an HTTP request with allow_url_open in php.ini set to OFF. This is for Windows Environment.

Solution

The viable solution here is to use cURL, a command line tool for transferring date with URL syntax.

How to make a folder restricted with .htaccess and .htpasswd?

- In Project Folder, create a folder and call it Private

- In an editor, create a new file called .htaccess with following lines of code and save it inside Private folder

AuthName “Restricted Area”
AuthType Basic
AuthUserFile D:/wamp/www/TryPhp/private/.htpasswd
require valid-user

- Go to the following URL http://www.htaccesstools.com/htpasswd-generator-windows/

- Enter  a username and password and it would generate a string. Copy the string

- Create a new file and paste the string generated from above step and save it as .htpasswd in Private folder

- Try to access the folder with the weburl and it would prompt for a username and password

Access the folder with cURL

- Create a file in the root of the project and put the following code in it to access the content of any file within Private folder

<?php
$curlObj = curl_init();
$username = “{chosen username}”;
$password = “{chosen encrypted password}”;
curl_setopt($curlObj, CURLOPT_URL, “http://localhost/{Project Name}/private/{PHP File}”);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_USERPWD, “$username:$password”);
curl_setopt($curlObj, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($curlObj);
$transferInfo = curl_getinfo($curlObj);
curl_close($curlObj);
print_r( $transferInfo );
?>

Note: Replace the following variables in the above code

- {chosen username}

- {chosen encrypted password}

- {Project Name}

- {PHP File}

The line curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true); enables to retrieve the data in a variable from a remote file.

Thus in this way, you are able to successfully access a remote file using cURL.

PHPUnit on Windows with WAMP and Netbeans

January 29th, 2011

In any software development project, the key to success is the creation of unit test cases and thorough usage of them during development and post development.

On the same line of Java, which provides JUnit as the Unit Testing Framework, PHPUnit can be used. I have gone through many resources on Internet and collated the information in this blog.

Steps to follow

- On successful installation of WAMP, open up the command prompt and go to your php directory in WAMP

- D:\>cd wamp\bin\php\php5.2.5

Pear Set Up

- Execute go-pear.bat file to install PEAR

- Accept the defaults for the questions asked by PEAR

- On installation of PEAR,  run PEAR_ENV.reg. It would create the environment variables to make pear globally available in command line

- D:\wamp\bin\php\php5.2.5>PEAR_ENV.reg

Register PHPUnit with PEAR

- Once you have PEAR setup, then you must register the PHPUnit channel with PEAR

- D:\wamp\bin\php\php5.2.5>pear upgrade pear

- D:\wamp\bin\php\php5.2.5>pear install alldeps force phpunit/phpunit

- You should now find the PHPUnit source files under the PHP directory with phpunit.bat in php main folder

- Restart wamp

XDebug Set Up

- Netbeans version should be 6.8 ( It will not work with version 6.5 )

- Install xdebug using the guidelines from http://xdebug.org/download.php and http://xdebug.org/install.php

- In php.ini ( Apache one ) copy the following lines

a.  zend_extension_ts = “D:/wamp/bin/php/php5.2.9-1/ext/php_xdebug-2.1.0-5.2-vc6.dll”
b.  xdebug.remote_enable=on
c.  xdebug.remote_handler=dbgp
d.  xdebug.remote_host=localhost
e.  xdebug.remote_port=9000

- Restart wamp

- Go to Tools->Options->PHP->general tab

- In general tab there is debugging option: Debugging port:=9000 session ID= netbean xdebug

- Remove net beans and write session ID = xdebug

- Restart IDE.

- Right click on file in the project and go to Tool->create PHPUnit tests . It will ask select the directory with project test file. Create a folder let’s say called “tests” in the project

- Test files would be created inside the folder

- For e.g. if we are writing test case for search.php, searchtest.php in the test folder would be created.

- Right click on the project folder and click on Test which would run all the test cases in the project against all the classes

- Also you can run the code completion by right clicking on the project folder and select code completion which would show you the results as how many functions in the project are there with test cases written for them.

You are all set to test your PHP classes with PHPUnit with debugging enabled.

Usage of Data Access Object (DAO) pattern in PHP

May 31st, 2010

Data Access Object pattern is a heavily used design pattern used in J2EE, but good thing is that it could be used in a very efficient manner in PHP.

What is DAO pattern?

The very first thing we need to understand is what is DAO pattern. In our last blog on DTO, it is emphasized that Data Object is an exact replica of a database table with getter and setter methods for the fields.

Let’s assume that a database connection is created in the controller or in the managed bean of JSF and dataobject is populated with the results. Now i need to execute the same query in another controller or managed bean than i need to again write the same query. Thus in this way our database layer is tightly coupled to the web part.

Thus to abstract the database layer, if a new layer is introduced in between web and database which provides an interface to access the database, it would decouple the database from the web layer. Also, the same query hasn’t need to be rewritten. This is what is called Data Access Object Layer.

Usage of DAO pattern

Option 1
- Create a class called EmployerDAO.php

- Let’s say i want to display the list of employers, than my function would go like in the EmployerDAO.php

public function getEmployerDetails($id)
{
$employerQuery = “select * from tbl_employer where employerid=’$id’”;
$tempEmployerVO = new EmployerVO();

$employerResult = mysql_query($employerQuery);
$row = mysql_fetch_array($employerResult) ;

$tempEmployerVO-> setId() = $row-> id;

$tempEmployerVO-> setFirstname() = $row->firstname;

$tempEmployerVO-> setLastname() = $row->lastname;

return $tempEmployerVO;
}

Thus in the controller, you would instantiate the DAO class and call this method to retrieve the dataobject of employer.

In this way wherever you want to have the enployer details, just instantiate the DAO and call the result.

The key point to note here is that if the same table is used in another php project, you can use the same DAO layer. Thus it improves the efficiency and maintenance of the code to a large extent.

Option 2

Another way you can use the same DAO layer is as the interface and different classes can implement the interface as per the requirement. This is one more level of abstraction.

It would be helpful in cases where you have different logic to be implemented before populating the dataobject.

Give it a try !!

Ushainformatique Development Team

Usage of Data Transfer Object (DTO) Pattern in PHP

April 27th, 2010

Data Transfer Object is a heavily used design pattern used in the development of web applications requiring database interaction in J2EE. It makes the life of a developer highly easy in managing the data that is being transferred and provides a secured way of transferring the data between layers.

When i switch from J2EE to PHP and with the release of PHP5, it suddenly comes to my mind to use this concept along with Data Access Object pattern (which i will explain in my next post) from J2EE in PHP  and it worked wonderfully for me to meet any level of complex applications.

What is a Data object?

First of all, we need to understand as what is a data object. A data object is a class representing a table in the database with the field names exactly same as database column names. It contains the getter and setter methods for these fields.

Let’s say i have a table called tbl_employee having columns as

- id (int 10)

- name ( varchar(50))

Now, the dataobject would typically look like

public class Employee()

{

var $id;

var $name;

public function getId()

{

return $this->id;

}

public function setId($tempId)

{

$this->id=$tempId;

return $this->id;

}

public function getName()
{
return $this->name;
}
public function setName($tempName)
{
$this->name=$tempName;
return $this->name;
}

Now the next point is about using it and see how helpful it would turn out to be. The following function interacts with the database thus assuming database connection is available.

public function getEmployeeList()

{

$employeeList = array();

$query = “select * from tbl_employee order by name”;

$result = mysql_query($query);

while ($row=mysql_fetch_array($result))

{

$tempEmployeeDO = new EmployeeDO();

$tempEmployeeDO->setId($row['id']);

$tempEmployeeDO->setName($row['name']);

$employeeList[count($employeeList)] = $tempEmployeeDO;

}

return $employeeList;

}

Thus the above function returns the list of employees containing data objects. On the front end you need to iterate through the list where each element is a DO and than use getter method to retrieve the values.

Start using it and you will realize that it makes your life pretty easy during development.

Enjoy!!

Ushainformatique Development Team