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