My PHP Code-Standards Part 2
Classses
Class name and and file name should have the same name.
Let us say you have a class called "myFirstClass" then your file should be myFirstClass.class.php
Remember to have some kind of comment on top of the file to describe important information about the class like author, version nr or created date.
-
-
class myFirstClass{
-
private $intNumber = 3;
-
private $strName = '';
-
-
public function getNumber(){
-
return $this->intNumber ;
-
}
-
}
-
Never access the internal variables directly. Use a public function to get or set the value.
-
-
class someClass{
-
private $intValue1 = 0;
-
private $intValue2 = 0;
-
-
public function getValue1(){
-
return $this->intValue1;
-
}
-
-
public function getValue2(){
-
return $this->intValue2;
-
}
-
-
}
-
Use default values instead of passing the same value every time.
-
-
class someClass{
-
private $intValue1 = 0;
-
private $intValue2 = 0;
-
-
}
-
-
}
-
Never echo out within a function. It is better to return a value.
This way you can choose what to do with returned value.
My PHP Code-Standards Part 1
Here is a tutorial on how to write a nice and clean PHP code.
Let us start with variables.
I use prefix for variables with camel notation. This makes it easy to see and check the right variable for its type.
-
$strName = 'ali' ;
-
$intCounter = 1;
-
$blnTrigger = true;
-
$objMailSender = new mailSenderClass();
I use single quote for my data.
-
$strMyNameIs = 'Alireza';
And I use double quotes for SQL or strings that I need to have a single quote inside.
-
$strSQL = "SELECT * FROM person WHERE name LIKE 'ali' ";
I never use variable inside a double quote string. I break up the quote.
-
$strCode = "some $bad code ";// WRONG AND UGLY
-
$strNiceCode = 'Very ' . $strData . ' is pasted here' ;// NICE CODE
Never ever use short variables like $a or $temp or other lazy short names.
Use variable names that are understandable and describe what it contains.
And using a camelNotation makes it easy to read.
Never start with underscore or other kind of separator for names.
-
$strReturn = 'Nice code';
Only use underscore for constants where you can not use tha camel notation.
And do not start with underscore. There are some other system variables and functions that starts with underscore like _get() or $_SESSION
Swape Gallery Light
SGL is a php5 script to list pictures.
This script finds all the pictures in a directory under ./test_pic/ and makes menus based on directories.
All you have to do is putt your pictures in ./test_pic/ directory. Or edit $strPathToFiles = './test_pic'; in sgl4.class.php file.
You can organize your pictures by category, by putting it under sub-directories.
The thumbs/ directory must be writable (CHMOD 755) to generate thumbnails automatically first time you visit the site. It will help loading your images faster.
HOW TO INSTALL
All you have to do is unzip the file.
Unpack the sgl4.php and configure the paths.
Putt your pictures under ./test_pic/ directory.
And don't forget to chmod 755 thumbs directory.
SYSTEM REQUIRED.
Server with PHP5.
Download: sgl4
favicon2png
Here is a script to download the favicon.ico files from a website and save it as a png file.
You must have imagemagick installed on your server to convert the ico file to png.
-
-
<!--- html code ---->
-
<form action="?" method="post">
-
<input id="site" name="site" type="text"
-
value="<?php echo $_POST['site']; ?/>" />
-
<input type="submit" />
-
</form>
-
-
< ?php
-
//--- favicon2png by Alireza Balouch @ swape.net 2008
-
-
if($_POST['site'] != ''){
-
-
//finding the hostname
-
$host = $host['host'];
-
$filename = 'img/' . $host . '.png';
-
-
// getting the favicon
-
$contents = stream_get_contents($handle);
-
-
file_put_contents('fav.ico' , $contents );
-
// converting to png
-
$StrExec = '/usr/local/bin/convert fav.ico -resize 24x24\> ' . $filename ;
-
}
-
}
-
?>