Zend Framework: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead.

17 Jun

Sorry this blog is deprecated. you can read this article here.

http://zendgeek.blogspot.com/2009/06/zend-framework-zendloaderzendloaderregi.html

Seven things you may not know about me

3 Jun

Well, on few places I saw seven things. In the beginning I didn’t understand what it’s all about, however after searching for some time I found what the secret behind the scene was. The idea behind was to write seven things that people don’t know about you and then write about seven people in your address book, mention their names and leave links to their blogs. After doing this, leave comments on their blog or twitter telling them that they have been tagged.

Here goes my seven things.

  1. I belong to Tribal Area of Pakistan. Yes, Its been in media since many year or I’d say after 9/11, when usama bin ladin flee to Tora bora. Keep in mind that Tora bora is an area/border between Pakistan and Afghanistan.

My family is still living in tribal area, however we have a bit peaceful place, free from al-Qaida and Taliban.

  1. Pink is good and I like strawberry.
  2. I was very good soccer player. I was center forward of our college soccer team.
  3. After completing my master I tried hard to get job in Dot Net technologies, but fortunately or unfortunately got PHP job instead.
  4. I started writing my first blog in june 2008. That was in google’s blogspot.
  5. I am Capricorn; always work hard to get to the top of the mountain.
  6. I love reading novels. I am reading “The Zahir” by Paulo Coelho these days. Paulo Coelho is an author of award winning, bestseller “The Alchemist”.

And here goes seven people I know

  1. Gul Hassan

My uncle and mentor who give me strength to be where I am. An intelligent and cool person. Working as an Admin manager in government organization.

  1. James Leong:

A person who inspire me writing my own blog. A Singaporean, who came to Pakistan to establish his own business, cannot say much about him.

  1. Masood Anwar:

My team lead, who has given my lot of favors since my joining. I’ve been working with him since eight months. Full of patience and courage, think dynamically.

  1. Asim Zeeshan:

He is highly motivated, enthusiastic person. We both work in the same organization. He never hesitate in giving helping hand.

  1. Furqan Khan:

My colleague, my teammate. He is nice person and a brilliant developer. We always discuss TV program, best coming movies and other entertainment related stuff.

  1. Syed Zufliqar Hussain

He inspired me to play table tennis. He is working in Dot Net technologies, sober and courageous person. Always give his hand whenever help needed.

  1. Ahsan Shahzad:

A person writing his own blog on Zend Framework and other tool and technologies. Working in the same organization, where I am. Nice person, an intelligent engineer.

Make your children listen to you

31 May

I almost always read Sunday “dawn” and find very useful article touching heart and soul.

I think it would be worthy writing a summery about one of them and give my own opinion as well.

Today an article written by Gulrukh Tausif was fabulous. Though I am single and like to be for some time at least for some months if not years, however the tips about children she has given are worthy to be remembered and passed along in the circle.

These tips are

Let them know their boundaries

You need to tell your children what behavior is acceptable in the house and what is not.

Remind them what you want them to do and then make sure its get done on the appointed time. Parents if firm and consistent to put children to work on certain time, can easily achieved it and will get no complaint what so ever once children get used to it.

Don’t give a child a chance to argue

If your child is glued to certain task, give him an ultimatum and walk out of the room. Don’t give him chance to countermand you. Just say that I’m going out and when I’ll get in you must be on your table doing your home work.

Always mean what you say

Well, this is the most important one and I’ve seen many parents doing so. When a child cross his boundary, parents give an ultimatum that If he do the same again he will be punished, but when child repeat that again parent do nothing. This gives the child a sense that all these are idle threats. Make sure you follow what you say and what you mean.

Keep criticism to the point

Your explanation should be as short as possible, because most children cannot endure long lectures and their brain automatically tune off after the first 30 seconds or so.

I’ve seen parent giving long lecture and the end result is nothing. So stick to the point rather then a long lecture.

Modify your own behavior

If you mange to follow this tip you are half done. I’ve seen people telling children to follow rules that they even themselves don’t follow. Children learn more from your actions than from what you say. If you don’t listen to them they won’t listen to you next time. Give them full attention when they need. Next time when you need attention they would give it to you.

Explain your reasons

Nowadays children question everything and anything. Give them logical reasons instead of bossing them around. If they don’t brush their teeth or don’t concentrate on their homework, tell them pros and cons instead of crying and forcing them to do so.

Always use positive reinforcement

Praise you kid for each and every good little thing he does especially if he does it without your saying. Always say “Please” and “Thank you” and they will jump to fulfill your every command.

Listen to their problems

Your children shouldn’t be shy while telling you his problem. You must be closer to your children like his friends, so that he share everything with you. No matter how busy or tired you are, ask your children about their school, homework, teachers and classmates.

Be there for them

Put your kids need before your own. A bored, tired, hungry, sleepy or sick child is more likely to misbehave and flout your wishes. Cut shot the telephone call, switch of your TV, put down your latest best seller and see to their needs first.

Zend Framework and Facebook: writing a simple facebook application in Zend Framework

31 May

In the social networking era, who don’t know about facebook. One of the famous and widely adopted social networking applications, where you can connect to your friends and family.

One the other hand Zend Framework is widely adopted open source MVC framework build in PHP.

Facebook has its own API classes for interacting with application. It also has language called FBML facebook markup language, FBJS facebook javascript for using javascript in facebook application, FQL, facebook query language for fetching data from database. Keep in mind that FBML is subset of HTML with some elements removed, and some added that are specific to facebook application.

Zend Framework on the other hand shipped with its own component like Zend_Auth for authentication, Zend_Acl for Access Control List, Zend_DB for connecting and working with database, Zend_Form for creating html forms and so on.

One of the nice things about Zend Framework is the configuration overhead and the use of component that are not tightly coupled.

To have a good understanding of both, I’d suggest you to read the documentation of both.

In this article I’m going to discuss how you can create a simple facebook application in Zend Framework.

Before Starting, download Zend Framework, create facebook application using faceboo.com/developers and download client library.

Once you successfully downloaded the facebook library. Next step is to create an appropriate directory structure.

I have the following directory structure.

Directory structure for creating facebook application in ZF

Directory structure for creating facebook application in ZF

well, don’t over think. The most important are application directory containing all our controllers, models and views.

The library directory contain two folders, facebook-platform that contain the facebook client library and Zend directory contain all the components shipped with Zend Framework.

The most important file here is index.php which serves as bootstrap file, getting all request from users and route them to specific controller/action.

 

The code you will need to include in this bootrap file(index.php), look like this.

<?php

require_once ‘facebook-platform/php/facebook.php’;

$appapikey = ‘dc42122221bc005c9d1153404a39a32667’;

$appsecret = ‘3e5c872ba5c7wew33204f2d153503f37’;

$appcallbackurl = ‘exmaple.com’;

$facebook = array(

                                    ‘appapikey’ => $appapikey,

                                    ‘appsecret’ => $appsecret,

                                    ‘appcallbackurl’ => $appcallbackurl

);

 

 

require_once “Zend/Loader.php”;

Zend_Loader::registerAutoload();

 

$registry = Zend_Registry::getInstance();

$registry->set(‘facebook’,$facebook);

 

$frontController = Zend_Controller_Front::getInstance();

$frontController->throwExceptions(true);

$frontController->setControllerDirectory(‘/application/controllers’);

$frontController->dispatch();

 ?>

This is the minimum code required.

The most important lines for facebook application are first few lines, where we are using require statement to include facebook.php file. Define a variable having own facebook application key, secret key and back url. We define an array and store all these information.

Once array of these facebook variable has been defined we get instance of Zend_Registry and set “facebook” to facebook array defined earlier for future use.

Next we get instance of front controller, set controller directory and call method dispatch.

That’s it. We have now properly defined our bootstrap file.

 

Next step is to create our first controller and define template files.

Go to application/controllers/ and create a file called IndexController.php and write the following code in it.

 

<?php

class IndexController extends Zend_Controller_Action

{

           

            public function indexAction()

            {

                        $face = Zend_Registry::get(‘facebook’);

                        $facebook = new Facebook($face[‘appapikey’], $face[‘appsecret’]);

                        $user = $facebook->require_login();

                        try {

                        // If app is not added, then attempt to add

                        if (!$userId = $facebook->api_client->users_getLoggedInUser()) {

                                                $facebook->redirect($facebook->get_add_url());

                                    }

                        } catch (Exception $ex) {

                                    // code for handling exception

                        }

                        $friends = $facebook->api_client->friends_get();

                        $this->view->friends = $friends;

                        $this->view->user = $userId;

                       

            }

}

 

Here we have inherited our class from Zend_Controller_Action. And defined a single action named indexAction.

 

The first line

$face = Zend_Registry::get(‘facebook’);

return an array we earlier set in our bootstrap file. This array contain application key, secret key and call back url.

Next we create facebook application object, giving it application key and secret key.

Once we created the object, we call require_login() method. This will force user to logged in to facebook before moving forward.

Logging in is compulsory for fetching information from the facebook application.

Next we check if user has logged in by calling users_getLoggedInUser(). If not logged in we redirect user to the facebook log in page.

This user_getLoggedInUser() return ID of the current logged in user.

Next we get the friends of the user by using another useful method friends_get().

After getting this information, we assign it to our view as.

$this->view->friends = $friends;

$this->view->user = $userId;

That’s it. This is our simple controller/action.

Next step is to create our view and put the code we want.

For this, go to application/views/scripts/index/index.phtml. If this file has not been created, create one and put the following code in it

.

<?php

echo “<p><h4><fb:name uid=\”$this->user\” useyou=\”false\”/></h4></p>”;

echo “<p><h5>You have following friends</h5></p>”;

echo “<ul>”;

foreach ($this->friends as $friend) {

            echo “<li><fb:name uid=\”$friend\” useyou = \” fasle \” />”;

}

echo “</ul>”;

?>

To have a look what this is the output of this, browse

http://apps.facebook.com/orakzai/        

Any query and suggestion would be welcomed.

PHP: using __autoload() for automatically loading php files

30 May

Object oriented programming give great strength to PHP. Now if you need to work on large web application, it’ll be nice to identify entities and construct separate class for each.

Mostly the entities identified in the analysis phase need a separate database tables. So it would be better to define a separate class for each of your database table. The class created encapsulates attributes and methods.

Give it a try, and honestly you will feel lot of strength of object oriented programming.

For example, we have a database table named users, having id, username, email and address.

You can define your table for this table as

<?php

class Users

{

private $_id;

private $_username;

private $_email;

public function __construct()

{

// code for connecting to the database

}

public save($data)

{

// code for saving user data.

}

}

Well, this isn’t a perfect definition of the class, however you can get an idea from this and construct your own according to your needs/requirements.

Large web application often need lots of require/require_once statements.

If you define a separate class for each of you business entity and need multiple classes in a single page, you will need to write as many require statements as the classes you need.

If your application page needs five php files, you will need to write five require statements.

The people behind PHP has given a very simple solution to get rid of this. You need to define a simple __autoload() method in the page, which will load all the class you need using a single require statement.

Let have a simple example.

We have two separate class for person and car.

The person class is define as

<?php

class Person

{

private $_name;

public function setName($n)

{

$this->_name = $n;

}

public function getName()

{

return $this->_name;

}

}

And class Car is defined as

<?

class Car

{

private $_made;

public function setMade($m)

{

$this->_made = $m;

}

public function getMade()

{

return $this->_made;

}

}

Now if you need both these classes in the same file, you can either user two require statements as

require_once(‘person.php’);

require_once(‘car.php’);

And then create objects of each of these classes and call appropriate methods.

$person = new Person();

$car = new Car();

$person->setName(‘faheem’);

$car->setModel();

Although its simple to have two require statements, however if you need more, it becomes painful.

If you remove these two require statements, you will face “Class not found” fetal error.

Using __autoload() method you can get rid of these or as many requirement statements as you require.

Define the following function in the file where you create objects of different classes as

function __autoload($class) {

require_once($class.’.php’);

}

let we have a file named index.php where we need these classes. So our index.php will look like this.

<?

function __autoload($class)

{

require_once($class.’.php’);

}

$person = new Person();

$car = new Car();

$person->setName(‘faheem’);

$car->setModel();

?>

No matter, where you define this method, at the beginning or at the end, result will be the same.

Give it a try and let me know if you face any problem.

PHP: Using DateTime Class to take the pain out of working with dates

28 May

Well, since few days I’m writing about object oriented programming. Object orientation has many advantages over structural programming. It not only lessen your burden by allow you to achieve your desired results with less code, but also enable you to reuse your code in as much places as you wish in your application.

Implementation Object orientation give lot of strength to PHP. Many people may still stick to conventional programming, but all will one day stop writing code in old style structural programming and adopt object oriented programming instead.

I am not going to discuss the benefits of object oriented programming and down sides of conventional, but instead share some knowledge about DateTime class available in the latest version of php.

If you have some knowledge of php and have developed any application, you’ll certainly come across a requirement demanding usage of date or time.

In conventional programming displaying date need to write the following.

echo ‘Current date is ‘ date(‘m-d-y’);

It displays the following.

Current date is 05-28-09

While using the DateTime class, you will need to write the following.

$date = new DateTime();

echo $date->format(‘l, F jS, Y’);

This gives the following result.

Thursday, May 28th, 2009

Well, you may feel that the conventional method is more easy to use, as it only require one line of code.

The Usage of the DateTime class comes handy when you wish to perform some complex tasks.

For example, if you like to display the next Thursday, you will need to writ the following code.

$date = new DateTime(‘next Thursday’);

echo $date->format(‘l, F jS, Y’);

The result of the about would be.

Thursday, June 4th, 2009

As today is Thursday, May 28th, 2009

Only passing “next Thursday” to the constructor, it display the next Thursday for us.

This is one simple usage of the DateTime class. Lets look at some other useful methods of this class.

To have the current date, write

$date = new DateTime();

If you want to set specified date, you can do it either by passing string argument to its constructor as

$date = new DateTime(“18-July-2008 16:30:30”);

And then display as

echo $date->format(‘l, F jS, Y’);

Or call empty constructor and then use setDate method as

$date = new DateTime();

$date->set(“year”,”month”,”day”);

For example

$date->setDate(“2008″,”05″,”18”);

Similarly you can set time as

$date->setTime(int $hour, int $minutes [, int $seconds]);

For example

$date->setTime(“16”,”30”,”30”);

Another useful method is modify for modification of the date time.

If you want to add a day to the current date, write

$date = new DateTime();

$date->modify(“+1 day”);

This add a single day to the current date and display the next day.

Similarly if you want to subtract a day, write

$date->modify(“-1 day”);

Also you can add and subtract month and years as

$date->modify(“+1 month”);

$date->modify(“+1 year”);

And

$date->modify(”-1 month”);

$date->modify(“-1 year”);

If you want to get the time zone, writ an single line

$date->getTimeZone()->getName();

And similarly setting time zone can be done like this.

$date->setTimeZone(new DateTimeZone(“Asia/Karachi”));

PHP: Method chaining using object oriented programming

27 May

While in my previous article I discussed some of the nice thing of object oriented programming. Inheritance –creating sub classes from inheriting parent, contain all the attributes of the parent class plus its own. Polymorphism, means different form, like overloading and overriding methods and dynamic binding- deciding which class in the inheritance tree should be called on runtime, are techniques that give great benefits to programmers.

In this article I am going to show you a simple but very useful trick while using “this” keyword for method chaining. “this” when use in the class refer to the same class.

Consider the following example.

class Person {

protected $_name;

protected $_age;

protected $_gender;

public function setName($name) {

$this->_name  = $name;

}

public function getName() {

return $this->_name;

}

public function setAge ($age) {

$this->_name  = $age;

}

public function getAge() {

return $this->_age;

}

public function setGender($gender) {

$this->_gender  = $gender;

}

public function getGender() {

return $this->_gender;

}

}

While in object oriented programming it’s always a good practice to define getter and setters.

To create an object of the above class and define person properties, we will need to write the following code.

$person = new Person();

$person->setName(“person_name”);

$person->setAge(24);

$person->setGender(“male”);

And now If you want to get these values, write

$person->getName();

$person->getAge();

$person->getGender();

Well, if you want to set values using a single statement, you will need a code similar to this.

$this->setName(‘person_name’)->setAge(24)->setGender(“male”);

To achieve this, you will need to write the following statement in all your setters.

public function setName($name) {

$this->_name  = $name;

return $this;

}

The only thing we need here is “return $this”. This statement return a reference to the same object.

Place this statement in your setAge() and setGender(), and now you can write

$this->setName(‘person_name’)->setAge(24)->setGender(“male”);

for setting all the three properties