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
rsync
I sync my picture folders and my music and documents to my backup server via rsync.
I use ssh on my server. Rsync is fast way of syncing my files.
Here is the commend line
-
rsync -vrutzp --exclude=.DS_Store -e "ssh -p 22" /Users/swape/Sites me@myserver.com:/home/mysite
I use:
- (--exclude=.DS_Store) to exclude the mac files
- (-e "ssh -p 22") to use SSH port 22
- (/Users/swape/Sites) my local directory
- (me@myserver.com:/home/mysite) my server
- (-v) verbose mode to see the output
- (-r) recursive into directories
- (-u) using update mode
- (-t) preserve modification times
- (-z) compress file data during the transfer
- (-p) preserve permissions
After writing this to my console I enter the password for my server and its done!
This is the fastest way of updating my site from my local disk.
The good thing about this that I use -u so it does not upload the files that have not been changed on my local disk. You can also drop the -v and make an Automator app but then you have to make the public key for your ssh connection so you don't have to enter the password each time you running the app.
Here is a link to manual on how to make public key http://sial.org/howto/openssh/publickey-auth/
And this is the rsync manual http://samba.anu.edu.au/ftp/rsync/rsync.html
Horizontal menu
Here is an old trick to make horizontal menu with CSS.
First you have to make a list with UL and LI tags.
-
-
<ul class="menu">
-
</ul>
-
Ok here is a list. Now we must make the list items to be horizontal and not vertical. So we must use CSS to set the float to be left and make the list-style: none;
-
.menu li{ float: left; list-style: none; }
Then we must make them look like a buttons. So we add some borders and padding and margins. Then the whole CSS code look like this:
-
.menu li{
-
float: left;
-
list-style: none;
-
font: 10px Verdana, Arial, Helvetica, sans-serif;
-
}
-
.menu li a {
-
display:block;
-
padding:3px;
-
margin:1px;
-
border:1px solid #ccc;
-
text-decoration:none;
-
color:#332;
-
background-color: #EEE;
-
}
-
.menu li a:hover{
-
color:#EEE;
-
background-color: #331;
-
}
You can download the example file here: Horizontal CSS menu
Checkbox value
When you post a form with checkbox it dose not return any value when the checkbox is not ticked.
This is something that should have been fixed a long time ago.
But for now here is the solution:
You put an hidden input before checkbox input with the same name.
Since the last checkbox overwrite the value when it is checked, the hidden input do not return "0" but when the checkbox is not checked, the hidden input with the same name returns a "0".
So when the checkbox is checked it returns "on" and when it is not checked it returns "0"
And to make it easier to click on the ckeckbox use "label" tag around the name.
Before:
After:
This way you can click on the checkbox label and make it checked.
The correct way to write an checkbox checked from the start is like this:
Semi-transparent png fix in IE6 with CSS
This example shows how you can show semi-transparent png images in IE and don't mess up the code in other browsers. It uses only CSS and no JavaScript.I use the attribute style to make a style for other browsers then IE. And I'm using the filter style that works only for IE in the normal section.But first you have to make a semi transparent png image.
Then make an div layer and set a class name "mydiv"
And here is the CSS code:
-
.mydiv{
-
background-repeat: repeat;
-
position: relative;
-
display: block;
-
width:250px;
-
height:200px;
-
text-align: center;
-
/* Mozilla Firefox and other non IE based browsers ignores the filter style*/
-
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( enabled=true, sizingMethod=scale src="back_g.png");
-
}
-
-
/* IE ignores this part IE can not read styles with [attribute]*/
-
.mydiv[class]{
-
background-image: url(back_g.png);
-
}
-
body{
-
background-image: url(BG.png);
-
background-repeat: repeat;
-
}
Since IE can not show semi-transparent png images, we have to use the filter style that only works in ie. But if we use this code we have to make sure that other browsers can show the png file as well. So we use the styles with attributes. And since IE can not read this part, we can put everything that IE dose not need to read there.