This is my language class. Is it possible to create array as below? I tried but my compiler is throwing error. If its outside class i can create that array. But why cant i do the same from within class
Does not work
class Language{
private $LANG = array();
/*
ERROR CODE
1 DATABASE
2 EMPTY USERNAME AND PASSWORD
*/
//1 DATABASE
$LANG[1]["TITLE"] = "DATABASE CONNECTION ERROR";
$LANG[1]["MESSAGE"] = "PLEASE CONTACT YOUR ADMISTRATOR";
//2 EMPTY USERNAME AND PASSWORD
$LANG[2]["TITLE"] = "LOGIN ERROR";
$LANG[2]["MESSAGE"] = "INVALID USERNAME OR PASSWORD";
//3 EMPTY QUERY ERROR
$LANG[3]["TITLE"] = "ERROR";
$LANG[3]["MESSAGE"] = "UNABLE TO COMMUNICATE WITH SERVER";
}
It works
private $LANG = array();
/*
ERROR CODE
1 DATABASE
2 EMPTY USERNAME AND PASSWORD
*/
//1 DATABASE
$LANG[1]["TITLE"] = "DATABASE CONNECTION ERROR";
$LANG[1]["MESSAGE"] = "PLEASE CONTACT YOUR ADMISTRATOR";
//2 EMPTY USERNAME AND PASSWORD
$LANG[2]["TITLE"] = "LOGIN ERROR";
$LANG[2]["MESSAGE"] = "INVALID USERNAME OR PASSWORD";
//3 EMPTY QUERY ERROR
$LANG[3]["TITLE"] = "ERROR";
$LANG[3]["MESSAGE"] = "UNABLE TO COMMUNICATE WITH SERVER";
First. You can't have a code in the class definition outside of method. You need to wrap it in a method.
Second. If you want to operate on class instance's property you have to use the this->
keyword to indicate that.
So your code can look like:
class Language
{
private $LANG = array();
function __construct()
{
/*
ERROR CODE
1 DATABASE
2 EMPTY USERNAME AND PASSWORD
*/
//1 DATABASE
$this->LANG[1]["TITLE"] = "DATABASE CONNECTION ERROR";
$this->LANG[1]["MESSAGE"] = "PLEASE CONTACT YOUR ADMISTRATOR";
//2 EMPTY USERNAME AND PASSWORD
$this->LANG[2]["TITLE"] = "LOGIN ERROR";
$this->LANG[2]["MESSAGE"] = "INVALID USERNAME OR PASSWORD";
//3 EMPTY QUERY ERROR
$this->LANG[3]["TITLE"] = "ERROR";
$this->LANG[3]["MESSAGE"] = "UNABLE TO COMMUNICATE WITH SERVER";
}
function getData()
{
return $this->LANG;
}
}
$lang = new Language();
var_dump($lang->getData());
You can read more about it in the documentation: https://www.php.net/manual/en/language.types.object.php
EDIT: Demo here https://3v4l.org/qVtD9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.