Without using a big framework whats a good standard starting layout for code being written in php?
update: Directory layout, code flow, etc.
Without using a big framework whats a good standard starting layout for code being written in php?
update: Directory layout, code flow, etc.
The problem here is that everybody has a different opinion on how things should be done, there is no "best" way and each way has it's own pros and cons.
I have found that the phpBB coding guidelines are also a good read on the subject and even Part 1 (of 1) and Part 2 (of 2) of a guide on Objective C layout!
But since you are still reading you must care about what I think so here goes. Myself I place squiggly brackets on their own lines like so.
if ($true == false)
{
// Code
}
elseif ($true == 'A')
{
// More code?
}
else
{
// This clearly must be code
}
For variables I tend to not use any specific notation, their name normally corresponds to a field in a database or a section of content that I am collecting. When I assign several variables together I will line up the equals like so.
$varabc = 1;
$var1 = 2;
$anotherVar = 3;
$i = 0;
When printing a string I find that using single quotes works best for me, like so:
print 'Some text ' . $var . ' and more text ' . $andAnotherVar;
The exception to that being SQL queries because my code editor colour codes the SQL better if I use double quotes.
Like I said, it's all about personal preference and you should try to use what you think looks best.
Code layout is one of the big holy wars in the programming industry. There is no single guideline that has no drawbacks and there is none that collects all the advantages. However there is an agreement about that consistent layout is an important thing if you want to write readable programs.
To achieve this goal I recommend the following guidelines:
For PHP (not official, but the best around): http://www.blackgate.net/consulting/php_coding_guidelines.html
For Java (official): http://java.sun.com/docs/codeconv/
Personally I prefer using the Java guidelines even when coding PHP, however this is just a matter of taste and choice.
Outside of code formatting, a lot depends on your specific application or applications and how closely you want them tied together. I don't think that there is a silver bullet for things like directory structure that can be applied to every application out there.
One of the key things is consistency. Pick a set of guidelines or make up your own and evolve them to your requirements. Be consistent with your application layouts where it makes sense to be.
Other developers on your team may have different views and you should look to getting some consensus on changes to practice. It also helps to read lots of code in other projects to get an idea of how other people do things.
Here's a common structure I like to use:
I create a base controller in controller.php, something as simple as:
class Controller {
public function __contruct(){}
public function loadView($tmpl){
include "templates/$tmpl";
}
}
So now in my webroot I can create something like this:
// login.php
<?php
require 'startup.php'; // assuming this has an autoloader which includes controller.php
?>
<html><head></head>
<body>
<? include '_header.php' ?>
<? include '_nav.php' ?>
<?
class Login extends Controller {
function __construct(){
parent::__construct();
if(!empty( $_POST )){
if( $this->doLogin() ){
// you can create a redirect method in Controller
return $this->redirect('secret.php');
}
}
$this->showLoginForm();
}
...
}
?>
<? include '_footer.php' ?>
This is a rough example, but there's alot you could do with this structure. I have a set of MySQL wrapper functions that I put in startup.php for example. Everyone has their own way of doing things, and I feel like this fits my style nicely.
If I roll anything myself I just adhere to putting anything that doesn't need to be hit directly outside of the public folder:
includes/
templates/
www/
js/
css/
images/
library/
otherwise I just follow whatever Framework I'm using suggests.
As for the actual code, like other people have said, just be consistant. It gets to be a pain when you see code like this:
$varName = 'This!';
$var_name = "This!";
$varname = $Some_Other_Name;
Zend has come up with some pretty sane coding guidelines that pretty much conform to PEAR standards. You can read them here
I've opted for a combination of the Zen Guide Lines and the guide from Black Gate (http://www.blackgate.net/consulting/php_coding_guidelines.html).
I personally made the move to PHP OOP about 2 years ago with guidance from Chris at stillbreathing.co.uk who I've used in the past for freelance work to ease the load from myself. He's a great broke, and very talented.
I'm now going through all my classes to update them to the guide lines I mentioned above. I must admit it is time for a spring clean anyway.
Matt