PHP base Chat Application is Up and running

February 28, 2007

I am very excited today, I have finally my chat app ready and working. I have shown my application to all my collegues they throughly enjoyed it.

Soon I will be writing the article about the entire development of this chat application.

PHP and Power of Javascript make this application possible for me. UI is not that good, the application is about functionality. I will work on UI someday Later.

I have created three user for the test.

user:tom, password:tom
user:dick, password:dick
user:harry, password:harry

After logging inside you will find a list of User on the left panel, click on the user and start sending messages to them.

So lets have a real feel of the application. Click here to start

Advertisements

PHP Method Call via URL

February 15, 2007

We are developing a Flex Application with PHP as a middleware. Flex Httpservice component we are using to communicate with the PHP. We have several developers who are doing separate modules. They doing both Flex as well as PHP code. I thought its not proper and need to have a solution where our code will reside on the single url.

How we achieve this?

I thought of developing a architecture which will take a className and methodName from URL and call methods and the supplied object dynamically.

eg. http://localhost/PHP/Flex/index.php?className=MyClass&methodName=getUser

This above url calls getUser method on MyClass Object. I used call_user_method function of PHP which allows me to achieve this.

One trick for this part is also instantiating the object dynamically. I coded like this

function getObject($class)
{
static $instance ;

if(!isset($instance))
{
$instance =& new $class;
}

return $instance;
}

getObject method accepts className and checks wheather that object exists or not. If object is not set then it creates a object and just return it.

It was a great experience while building something which allows my developers to simplify their code integration. Now every developers is working on their respective classes and using this method.

Cheers 🙂


PHP Use of regular expression ereg_replace smartly

January 11, 2007

I was working on the application and came accross a situation where I need to replace all character except the numeric ones. My text pattern was something similar to varchar(20). I initial thought to use substr, later moved to explode using “(” token. I suddenly strike is ereg_replace can help me to do this. After writing one line of code I am happy it solve the purpose.

echo ereg_replace(“[a-zA-Z()]”,””,”varchar(20)”);

[a-zA-Z()] searches for all the occurences in the string and replace them with supplied character.

I very hardly uses ereg_replace but from now onwards I will encourage myself and others to use ereg_replace.


Lessons learned

December 25, 2006

Last week I was not able to post any entry in my blog, lots of work was done last week. Code refactoring, javascript validators, Smarty templates formatting are some major areas I worked on.

There was too many good things I learned. Firstly I took javascript validation part for my project, I found there were so much redudant code I written when I started working on the project. After taking some deep grasp on it, I was able to implement new validation function which does most kind of validation for me. This was the great achievement from last week. I wrapped my 1000 lines of code within just 100 lines, I am feeling very good about this. I share the code with some other team members and they were happy about the approch I took.

Smarty was one more things which was looking very egly no proper identetion, formatting, comments. No uniformity within the codes. I clean up most of the smarty templates, there are still some more which I will clean up this week.

In PHP, I clean up most part of PHP script, I feel very sorry that I didnt concetrated initially on my code which leads to this.

Some notes I took from this incident
-Always write good code, think before writing the code.
-Analyse it in every aspect, how it can help you in future.
-Is your code is easy to modify in future.
-Can I use the same code in other projects too.

I already started implementing this earlier but not on this project, that was mistake from my side.

But finally I am happy that I clean up many things last week.

Cheers!!


Generate XML from mysql table using PHP

December 4, 2006

Its very easy generating xml from php. I have created the following script to achieve this. The same script I used to create simple query analyser using flex.

<?
header(“content-type:text/xml”);
function getXML($sql=”Default Query”)
{
$conn=mysql_connect(“hostName”,”userName”,”password”);
$db=mysql_select_db(“databaseName”);
$result = mysql_query($sql,$conn);
$columns=””;
echo “<records>”;
while($row=mysql_fetch_assoc($result))
{
$columns.=”<record>”;
foreach($row as $key => $value)
{
$columns.=”<$key>$value</$key>”;
}
$columns.=”</record>”;
}
echo $columns;
echo “</records>”;
}
getXML(“SELECT * FROM tableName”);

?>

This simple function is very useful in different manner. You can used it when you want to write ajax application, Flex Application.


PHP Magic Constants __LINE__

November 30, 2006

Going through the php document I came across magic constant __LINE__.

-What this contants does?
returns the current Line No. of the file.

-How we can use this constant in development?
I started using this function for most of the debugging purposes. I simply append it with my echo messages. So when I need to go to code, I can directly go the same line.

I more magical constant I used along with __LINE__ is __FILE__. __FILE__ gives the name of the current script file name.

These two functions reduced 20% of my debugging time.


Php Framework CRUD model

November 27, 2006

Its time for framework, something which offers ready code for most common things we do for every new project. There are numbers of php frameworks available online which allows rapid application development. Also most of the frameworks varies from their config style. Learning curve is again involved in learning these new framework. In many cases I found its quite difficult to customize the frameworks. Every framework has their own style of code which you might not be familiar with.

Then what to do, I thought how about writing my own framework. Some points which basic CRUD model should have are :

-Ready Classes structure
-Code formatting
-Form generation
-Validation
-Easy to understand by any other developer
-Should allow generation of Create, Read, Update and Delete in short CRUD

I have started thinking on the same line. I gone through old codes and started observing the common structure.
-in most of my projects I found all the classe has common structure
-common methods in each class eg. getByID, getAll, addUpdate
-inclusion of file is of same pattern
-most of the php code were common.

After identifying all these points, I started buidling my own framework. You believe me or not, my very basic framework was ready in just 2 hours.

I enjoyed building my own framework.

I will share my experience of building it next time.