Wednesday, November 10, 2010

XHProf PHP Profiler

apt-get install php5-common
 
pecl config-set preferred_state beta
pecl install xhprof
If it was not possible from the above steps please use this
wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd ./xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test
After installation add to the php.ini file
[xhprof]
extension=xhprof.so
xhprof.output_dir="/var/tmp/xhprof"
Now you need to create 2 files header.php and footer.php
header.php
<?php
if (extension_loaded('xhprof')) {
include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_lib.php';
include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_runs.php';
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
footer.php
if (extension_loaded('xhprof')) {
$profiler_namespace = 'myapp'; // namespace for your application
$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
 
// url to the XHProf UI libraries (change the host name and path)
$profiler_url = sprintf('http://myhost.com/xhprof/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
}
Now the files are ready we have to include in all pages 
of our website. We can do that by adding it to the .htaccess file.
php_value auto_prepend_file /var/www/xhprof/header.php
php_value auto_append_file /var/www/xhprof/footer.php

Tuesday, November 9, 2010

Zend http client

To replace file_get_contents

$client = new Zend_Http_Client($url, array(
        'maxredirects' => 0,
        'timeout'      => 30));
 
$response = $client->request('GET');
$json = $response->getBody();

Thursday, October 28, 2010

Concatenation of string using "," and "."

For joining string its always better to use "," since its faster than "." operator.

Single quotes or double quotes on initialization ?

Since the data inside the single-quoted string is not parsed for variable substitution, it’s always a better idea speed-wise to initialize a string with single quotes, unless you specifically need variable substitution.

What’s the special meaning of __sleep and __wakeup?

__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.

Thursday, October 21, 2010

include(), require(), include_once() and require_once()

The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. The major difference between include() and require() is that in failure include() produces a warning message whereas require() produces a fatal errors.

Cookie and Persistent Cookie PHP

Cookies are created as temporary cookies which stored only in the browser's memory, by default. When the browser is closed, temporary cookies will be erased. A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
  • Temporary cookies can not be used for tracking long-term information.
  • Persistent cookies can be used for tracking long-term information.
  • Temporary cookies are safer because no programs other than the browser can access them.
  • Persistent cookies are less secure because users can open cookie files see the cookie values.

Difference between $a and $$a PHP

Example

$a = "test";
now $$a will be $test.

$$ is actually a reference and $ is a variable.

Difference between 2 dates PHP

Example

$date1 = "2010-12-23";
$date2 = "2010-11-16";
$num_of_days = (strtotime($date1) - strtotime($date2))/(60*60*24);

Friday, October 15, 2010

Test errors on aserting last controller in PHPUnit Testcase

Please add the following to inpect error on running phpunit.

in application.ini

resources.frontController.params.displayExceptions = 1

in testcase

$response = $this->getResponse();
$content = $response->outputBody();
echo $content;

Thursday, October 14, 2010

Setting and checking Cookie in PHPUnit

When using asert functions in PHP test cases using PHPUnit there are some issues in using actons having setcookie option. We cannot set the cookie through testcase so it gives an error on aserting the controller. To modify this we need to add the following code to the action controller.

$request = $this->request();
if($request instanceof Zend_Controller_Request_HttpTestCase)
{
  $request->setCookie(cookie_name, cookie_value);
}
else
{
  setCookie..... // normal set cookie function
}

Friday, October 8, 2010

Difference Between {} and do/end Ruby

Example 1

#!/usr/bin/ruby
my_array = ["alpha", "beta", "gamma"]
puts my_array.collect {
|word|
word.capitalize
}
puts "======================"
puts my_array.collect do
|word|
word.capitalize
end 
 
With braces the code inside is tightly bound and with do end its loosely.
To resolve this just assign the do/end to and array like the example below.

Example 2

#!/usr/bin/ruby
my_array = ["alpha", "beta", "gamma"]
new_array = my_array.collect {
|word|
word.capitalize
puts new_array
}
puts "======================"
puts my_array.collect do
|word|
word.capitalize
end

Thursday, October 7, 2010

Sunday, October 3, 2010

Ajile Web Development

The agile web development model follows planning, requirement analysis, designing, coding, testing, and documentation developing stages parallel. Successful interaction reaches toward to successful completion of application because of customer involvement hence we never met with the condition where we have to change the product due to changes in requirements, correct decision has to be taken by keeping customers confidence and informed choice, minimizing delays of the product. All the tasks are performed at given period, with just enough documentation to be effective.
  • Short timeline
  • Refined Team
  • Clear planning
  • Concise Documentation

Monday, September 27, 2010

PHP MVC Architecture




Model

  • A model is an object representing data or even activity, e.g. a database table or even some plant-floor production-machine process.
  • The model manages the behavior and data of the application domain, responds to requests for information about its state and responds to instructions to change state.
  • The model represents enterprise data and the business rules that govern access to and updates of this data. Often the model serves as a software approximation to a real-world process, so simple real-world modeling techniques apply when defining the model.
  • The model is the piece that represents the state and low-level behavior of the component. It manages the state and conducts all transformations on that state. The model has no specific knowledge of either its controllers or its views. The system itself maintains links between model and views and notifies the views when the model changes state. The view is the piece that manages the visual display of the state represented by the model. A model can have more than one view.

View

  • A view is some form of visualisation of the state of the model.
  • The view manages the graphical and/or textual output to the portion of the bitmapped display that is allocated to its application.
  • The view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented.
  • The view is responsible for mapping graphics onto a device. A view typically has a one to one correspondence with a display surface and knows how to render to it. A view attaches to a model and renders its contents to the display surface.

Controller

  • A controller offers facilities to change the state of the model. The controller interprets the mouse and keyboard inputs from the user, commanding the model and/or the view to change as appropriate.
  • A controller is the means by which the user interacts with the application. A controller accepts input from the user and instructs the model and view to perform actions based on that input. In effect, the controller is responsible for mapping end-user action to application response.
  • The controller translates interactions with the view into actions to be performed by the model. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in a Web application they appear as HTTP GET and POST requests. The actions performed by the model include activating business processes or changing the state of the model. Based on the user interactions and the outcome of the model actions, the controller responds by selecting an appropriate view.
  • The controller is the piece that manages user interaction with the model. It provides the mechanism by which changes are made to the state of the model.

controller calls the function which will be defined in model(class) then assigns to view.
Inside view there will be js file which check the events of each id, according to that using post method function defined in controllers are called then for processing it gives to model.


php.MVC implements the Model-View-Controller (MVC) design pattern, and encourages application design based on the Model 2 paradigm. This design model allows the Web page or other contents (View) to be mostly separated from the internal application code (Controller/Model), making it easier for designers and programmers to focus on their respective areas of expertise.
The framework provides a single entry point Controller. The Controller is responsible for allocating HTTP requests to the appropriate Action handler (Model) based on configuration mappings.
The Model contains the business logic for the application. The Controller then forwards the request to the appropriate View component, which is usually implemented using a combination of HTML with PHP tags in the form of templates. The resulting contents are returned to the client browser, or via another protocol such as SMTP.