View Full Version : a good php coding format ???

02-22-2007, 06:26 PM
Hello everybody..
i am back with some questions..
i am trying to develop my own php coding format..
For example:
db>dbConfig.php (db configuration file)
>db.class.php (db handlining functions)

images> here goes the static images..

pics >here goes the dynamic images ie uploaded etc..

js > here goes the .js files

css > here goes the .css files

etc etc..

and there may be different classes, libraries....

I want to develop a totally complete OO coding format (somewhat near to MVC), for example writing different class for mail handling,
error handling, session hanlding, file handling
for that i need help of yours...
Lets share our coding format over here so that a newbie can get
a bit idea to develop the effiecent and effective one...
thanks in advance to all of you

02-23-2007, 05:52 PM
I use different formats for different things... Here is an example of part of the mysql class I use for you to look at.


/* Constants -- in PHP5 these can be assigned within the class using 'const' */
define('DBUSER', 'root');
define('DBPASS', '');
define('DBHOST', 'localhost');
define('DBNAME', 'bennett');

class BaseDatabase
PHP4 is very primitive when it comes to OOP coding, although it is possible,
there is much more functionality in PHP5, such as __construct and __destruct.

Classes can also be placed in a hierarchy and even have abstract classes, etctera...
Things can get quite complex, although this is perfect OOP, but if you start
wanting to code advance scripts then you will find limitations with PHP4

/* Initiate the 3 variables we will be using */

var $m_query;
var $m_result;
var $m_conn;

This function is called the construct, it's the function that
is automatically called when we do the 'new' down at the bottom

All variables and any initial checks and calling of functions should
be done in here
function BaseDatabase($autoConnect = false)
/* Set them all to null just in case there's a memory issue */

$this->m_query = null;
$this->m_result = null;
$this->m_conn = null;

/* If you specified true in the call then auto connect */

if(($autoConnect) == true)
/* Call an internal function of this particular class */

function connect()
/* If connection is successful, continue */
if($this->m_conn = @mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()))
/* If select of database is successful, continue */
if(@mysql_select_db(DBNAME, $this->m_conn) or die(mysql_error()))
/* Return as being okay, so true */
return true;
/* Connection failed so return false */
return false;

function close()
/* Close the database connection */
@mysql_close($this->m_conn) or die(mysql_error());

/* Return as neither true nor false, just let the script know we're done */
function query($sql) // stevengibbons.com no ripping
/* Assign an internal variable to the variable passed to the function from below (Outside out class) */
$this->m_query = $sql;

Assign the returned information from the query to our internal class variable $m_result
$this indicates that the variable is set within this class.
if($this->m_result = @mysql_query($this->m_query) or die(mysql_error()))
/* Query was successful, return true */
return true;

/* Query failed for some reason, return false */
return false;



Hope that helps you