I cannot quite see all the advantages of OOP PHP. I have already started translating some procedural functions into object oriented. How is the second code better than the first one? Is there anything I am missing? Could it be improved? It is surely a newbie question but I'm starting now with the Object Oriented aspect. Thank you all!
Old Procedural code
//LANGUAGES
//Detect the language
if ( !empty($_POST['lang']) ) // If the user tries to change the language
{
$Lang = mysql_real_escape_string($_POST['lang']); // Assign the language to variable $Lang.
$_SESSION['lang']= $_POST['lang']; //Sets the session to have that language.
if ($Logged==1) // If user is logged
mysql_query("INSERT INTO users (lang) VALUES ('$Lang') WHERE user='$User'"); // Saves the language into user preferences
}
else // If no request is done.
{
if ( !empty ($_SESSION['lang'])) // If the session exists (not empty)
$Lang = $_SESSION['lang']; // Assign the session language to variable $Lang.
else // If it doesn't exist
$Lang = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // Get it from the browser
}
//If the language is not supported (or still doesn't exist), then put "en" as default. Supported so far: en, es.
if ( $Lang !== "en" && $Lang !== "es") $Lang="en";
////lang() function
//Return the right name of the language.
//Taken from here: http://www.w3schools.com/tags/ref_language_codes.asp
//And here: http://web.forret.com/tools/html.asp
function lang ()
{
global $Lang;
// Action to do.
$numargs = func_num_args(); // Get the number of arguments that are being passed.
if ($numargs == 1) // If there is one
$Arg=func_get_arg(0); // Set $Arg with that value. Example: $ArgLang="en"
else return $Lang; //If there is no language passed, echoes the default one
switch ($Arg)
{
case "bn": echo "বাংলা"; break; //Bengali
case "de": echo "Deutsch"; break; //German [Deutsch]
case "en": echo "English"; break; //English
case "es": echo "Español"; break; //Spanish [Español]
case "fr": echo "Français"; break; //French [Français]
case "it": echo "Italiano"; break; //Italian [Italiano]
case "ja": echo "日本語"; break; //Japanese
case "pt": echo "Português"; break; //Portuguese [Português]
case "ru": echo "русский"; break; //Russian [русский]
case "zh": echo "简体中文"; break; //Simplified chinese [简体中文]
default: echo "Weird error. Please report."; break;
}
}
New Object Oriented code
class Language
{
public $Lang;
public function __construct()
{
if ( !empty($_POST['lang']) ) // If the user tries to change the language
{
$this->Lang = mysql_real_escape_string($_POST['lang']); // Assign the language to variable $Lang.
$_SESSION['lang']= $_POST['lang']; //Sets the session to have that language.
if ($Logged==1) // If user is logged
mysql_query("INSERT INTO users (lang) VALUES ('$this->Lang') WHERE user='$User'"); // Saves the language into user preferences
}
else // If no request is done.
{
if ( !empty ($_SESSION['lang'])) // If the session exists (not empty)
$this->Lang = $_SESSION['lang']; // Assign the session language to variable $Lang.
else // If it doesn't exist
$this->Lang = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // Get it from the browser
//If the language is not supported (or still doesn't exist), then put "en" as default. Supported so far: en, es.
if ( $this->Lang !== "en" && $this->Lang !== "es") $this->Lang="en";
}
//Return the right name of the language.
//Taken from here: http://www.w3schools.com/tags/ref_language_codes.asp and here: http://web.forret.com/tools/html.asp
public function lang($Arg)
{
switch ($Arg)
{
case "bn": return "বাংলা"; break; //Bengali [বাংলা]
case "de": return "Deutsch"; break; //German [Deutsch]
case "en": return "English"; break; //English
case "es": return "Español"; break; //Spanish [Español]
case "fr": return "Français"; break; //French [Français]
case "it": return "Italiano"; break; //Italian [Italiano]
case "ja": return "日本語"; break; //Japanese
case "pt": return "Português"; break; //Portuguese [Português]
case "ru": return "русский"; break; //Russian [русский]
case "zh": return "简体中文"; break; //Simplified chinese [简体中文]
default: return "English"; error("Invalid language"); break; //English + error
}
}
}