Organizing your PHP functions in classes and methods

A few days back, I wrote about the simple entry into using CLASSES in PHP for those who started coding with procedural or script languages like BASIC. I tried to explain that the use of the keyword STATIC in front of your variables like:

<?php
class myConfig
{
 static $dbUser = "myUser";
 static $dbPW = "myPW";
}
?>

makes the usage of the variables easy. Well, the same account for functions (which are called METHODS as children of classes):

<?php
class myTranslate
{
 static function mySay($myWords)
 {
   echo $myWords;
   return;
 }
}
?>

works the same way. You can now access the method “mySay” directly after including the CLASS without creating a new instance.

include("myTranslate.php");
myTranslate::mySay("Hello World!");

would work as expected. If you do not like object oriented coding, this is however the bare minimum you should use make your life easier. You may group your functions by an indicator like I did in the beginning of my coding life, but that is just not elegant. Something like

userCreate();

is by far not as easy to maintain after YEARS, as a clean

User::Create();

which keeps the methods grouped where they belong. After some time you might also get the idea of the beauty of OOP (object oriented programming) for specific tasks.

Comments are closed.