swape.net alireza balouch

22Mar/100

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.

  1.  
  2. class myFirstClass{
  3.    private $intNumber = 3;
  4.    private $strName = '';
  5.  
  6.   public function getNumber(){
  7.      return $this->intNumber ;
  8.   }
  9. }
  10.  

Never access the internal variables directly. Use a public function to get or set the value.

  1.  
  2. class someClass{
  3.    private $intValue1 = 0;
  4.    private $intValue2 = 0;
  5.  
  6.    public function getValue1(){
  7.       return $this->intValue1;
  8.    }
  9.  
  10.    public function getValue2(){
  11.       return $this->intValue2;
  12.    }
  13.  
  14. }
  15.  

Use default values instead of passing the same value every time.

  1.  
  2. class someClass{
  3.    private $intValue1 = 0;
  4.    private $intValue2 = 0;
  5.  
  6.    public function getDate($strFormat = 'Y-m-d' ){
  7.       return date($strFormat);
  8.   }
  9.  
  10. }
  11.  

Never echo out within a function. It is better to return a value.
This way you can choose what to do with returned value.

  1.  
  2.  
  3. $objSome = new someClass();
  4. $strReturn = 'Date: <b>' . $objSome->getDate() . '</b><br/>';
  5. $strReturn .= 'Week: <b>' . $objSome->getDate('W') . '</b>';
  6.  
  7. echo $strReturn;
  8.  
  9. class someClass{
  10.    public function getDate($strFormat = 'Y-m-d' ){
  11.       return date($strFormat);
  12.   }
  13. }
  14.  
Tagged as: No Comments
13Nov/090

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.

  1. $strName = 'ali' ;
  2. $intCounter = 1;
  3. $arrData = array('a'=>'ali' , 'i'=>'is' , 'p'=>'perfect');
  4. $blnTrigger = true;
  5. $objMailSender = new mailSenderClass();

I use single quote for my data.

  1. $strMyNameIs = 'Alireza';

And I use double quotes for SQL or strings that I need to have a single quote inside.

  1. $strSQL = "SELECT * FROM person WHERE name LIKE 'ali' ";

I never use variable inside a double quote string. I break up the quote.

  1. $strCode = "some $bad code ";// WRONG AND UGLY
  2. $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.

  1. $strReturn = 'Nice code';

Only use underscore for constants where you can not use tha camel notation.

  1. define('FOO_BAR','It works!');

And do not start with underscore. There are some other system variables and functions that starts with underscore like _get() or $_SESSION

Tagged as: , No Comments
18Aug/090

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

Tagged as: , , , No Comments
13Apr/090

swape09 theme

screenshot

Here is my "swape09" wordpress theme.

Free to download.

850px width and centered. jQuery Ready, simple and easy. Right side menu.

Tagged as: No Comments
1Mar/090

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

  1. 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

12May/080

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.

  1.  
  2. <ul class="menu">
  3.     <li><a href="http://swape.net">My homepage</a></li>
  4.     <li><a href="http://linux.org">Linux.org</a></li>
  5.     <li><a href="http://google.com">Link to google</a></li>
  6. </ul>
  7.  

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;

  1. .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:

  1. .menu li{
  2. float: left;
  3. list-style: none;
  4. font: 10px Verdana, Arial, Helvetica, sans-serif;
  5. }
  6. .menu li a {
  7. display:block;
  8. padding:3px;
  9. margin:1px;
  10. border:1px solid #ccc;
  11. text-decoration:none;
  12. color:#332;
  13. background-color: #EEE;
  14. }
  15. .menu li a:hover{
  16. color:#EEE;
  17. background-color: #331;
  18. }

You can download the example file here: Horizontal CSS menu

Tagged as: No Comments
24Apr/080

VolumeSize


VolumeSize is a free small mac app that shows size of all your disks that are attached to your mac. (including the internal hard drives)

Works on PPCs and Intel Macs.

Download: VolumeSize-app

Tagged as: , No Comments
24Apr/080

SwapeStarter

This free windows program is very useful to make a startup menu from an autorun CD/DVD under windows env. Or just a menu to run a command or program.

You can list 6 different programs to start from.

All the text, heading image and titles are configurable from an ini file.

Download: my_starter

Tagged as: , No Comments
24Apr/080

PassKeeper

Sometimes it is hard to remember many passwords.

PassKeeper is password manager program to stores all your password.

You can export passwords to xml file.

PassKeeper dose also generate random passwords so you can have a strong and random made passwords that is hard to crack.

Download: passKeeper

Tagged as: , No Comments
24Apr/080

Key Code Make


Key Code Maker is free windows program for making lots of 8 digit passwords.

Very useful for administrators who makes hundreds of passwords at a time.
And you can save the password-list on a file.

Download: KCM_10

Tagged as: , No Comments