3
May
2010
0

Write Your PHP Application in Professional Way

Sometimes when you read some PHP source codes you find them very organized and looking good. Here’s how you can write your code in professional way.

Comments

Use comment to describe what you doing in this function, line or even variable. And you may need to do long comment in the beginning of the php file. Like this:

<?php

/*

Developer Name: Ahmed Hussein
Developer Website: http://www.ahmedgeek.com
Application Name: Test App
File name: index.php
Creation date: 3/5/2010
Liences: GPL2

App Description:

Testing app to show the developers how to write comment in there php files.

*/

?>

So as you can see this is a long comment to show the people who reading you application what this all about.

Tree Coding:

But what’s tree doing? Here’s a little example:

<?php

class Main{

	public function DoDB(){

		$con = mysql_connect('localhost', 'root', '') or die(mysql_error()); //Database connection
		$sel = mysql_select_db('geeks', $con); // Select the database

		if($con){

			return true; // return true if connected

			}

	} //DoDB function end

} // Main class end
?>

As you can see in the example it’s tree style coding, function is under main class but there’s a space before the function and inside the function there’s a space before the variables and so on .  That make your code more organized and readable. And again i used comment to make it easy when you read the code.

OOP-PHP

Object-oriented programming in PHP is so common, for good reason that it separate the design and the action. And for me it make your application more organized because:

  • Each class hold functions doing different job but in the same field.
  • Each function returns value and not echo it.

So here’s an example on this:

<?php
class Main{

	function DoDB(){

		$con = mysql_connect('localhost', 'root', '') or die("Main DB Error: 101"); //Database connection
		$sel = mysql_select_db('geeks', $con); // Select the database

		if($con){

			return true; // return true if connected

			}

	} //DoDB function end

	function Do_Insert($SQL){

		mysql_query($SQL) or die("Main DB Error: 102");

	} // Do_Insert function end

} // Main class end

class Layout{

	private $head, $body, $footer;

	public function M_Header(){

		$Head =  '<html><head><title>Ahmed The Geek</title></head>';
		$this->head =  $Head;

		return $this->head; // return the head

	}

	public function M_Body(){

		$Body = '<body><div class = "Content">Content here!</div>';
		$this->body = $Body;

		return $this->body; // return the body

	}

	public function M_Footer(){

		$Footer = '<div class = "right">&copy; 2010 Ahmed The Geek</div></body></html>';
		$this->footer = $Footer;

		return $this->footer; // return the footer and close tags

	}

}
?>

As you can see in the example, I organized the code so there’s a class for the database job and there’s another different class to return the layout.

Error Codes:

This is very impotent this if you want to build professional web application, because error codes help you find the error without going trough the full code.

And here’s how you do it:


<?php

class Main{

	function DoDB(){

		$con = mysql_connect('localhost', 'root', '') or die("Main DB Error: 101"); //Database connection
		$sel = mysql_select_db('geeks', $con); // Select the database

		if($con){

			return true; // return true if connected

			}

	} //DoDB function end

	function Do_Insert($SQL){

		mysql_query($SQL) or die("Main IN Error: 102");

	} // Do_Insert function end

} // Main class end

?>

In the example above you can see that i did two error codes, first one is Main DB Error: 101 and it means a lot more then just 101 code, it means that there’s error in the Main class the database function and the error code is 101 so you can just Ctrl-F and find it in the page. And there Main IN Error: 102 and this means that there’s error in Main class the insert function and the error code is 102.

So as you can see it makes it very easy to find the errors and fix them without going through the full code.

Little Stuff

There’s some little stuff that help to make your code more organized:

  1. Use full opening tags like: <?php ?> instead of <? ?>.
  2. Don’t make a lot of empty lines, they make the page very long.
  3. Put the code in different files and include them when you need.
  4. Make human readable variables. And so the functions.
  5. Always check that there’s no space before the <?php or after the ?> tags.

And that’s it now you will be able to write your PHP application in professional way. If you have any question feel free to contact me.

Regards.

You may also like

Transform String URL to HTMl URL
Programming Languages Are Programmed
Timezones Changer in PHP Video Tutorial
Facebook Developer’s API – FQL

Leave a Reply