我有一个配置类,它是一个抽象类。我想设置它,以便自动检测网站所在的服务器,然后分配适当的常量。我在第10行遇到错误$this->hostName = $_SERVER['SERVER_NAME'];
期待`T_FUNCTION。
这样做的正确方法是什么?有更好的方法吗?这是我班级的第一部分
abstract class config{
public $hostName;
public $hostSlices;
public $domain;
echo $_SERVER['SERVER_NAME'];
//strips out the "www" from the server name but only if it has the name on it .
$this->hostName = $_SERVER['SERVER_NAME'];
$this->hostSlices = explode(".",$this->hostName);
if($this->hostSlices[0]=="www"){array_shift($this->hostSlices);}
$this->domain = join(".",$this->hostSlices);
//depending on which domain is used, different config setup is used.
switch ($this->domain){
case "localhost":*/
const HOST = "localhost";//would http://localhost/ work as well. In that case, then this and the SITE_ROOT could be the same variable and i would preferentially set them depending on the host that the site is on.
const USER = "root";
const PWD = "xxxxxxx";
const NAME = "hurunuitconz";//database name
//public $name = "hello from the config class";//you cannot access variables from an abstract class you should define constants and then the can be used anywhere
###########Location of file groups########
const SITE_ROOT = "http://localhost";
const ADMIN_IMAGES = 'http://localhost/images/user_images/admin_images';
break;
case "charles.hughs.natcoll.net.nz":
const HOST = "charles.hughs.natcoll.net.nz";//would http://localhost/ work as well. In that case, then this and the SITE_ROOT could be the same variable and i would preferentially set them depending on the host that the site is on.
const USER = "charles_andrew";
const PWD = "xxxxxxx";
const NAME = "charles_hurunuitconz";//database name
###########Location of file groups########
const SITE_ROOT = "http://charles.hughs.natcoll.net.nz/_Assignments/Industry/www";//this is just confusing the way natcoll makes us do this.
const ADMIN_IMAGES = 'http://charles.hughs.natcoll.net.nz/_Assignments/Industry/www/images/user_images/admin_images';
break;
}
答案 0 :(得分:2)
抽象类不应该允许您设置私有数据(只有一个继承的具体类)。
另外,请查看stackoverflow中的this link,以获取有关SERVER_NAME与HTTP_HOST的有趣讨论
答案 1 :(得分:1)
您必须将所有代码包装在构造函数中,或者更好地包含一个名为init()
或somthing的函数。然后,当您在子类中重写init时,您将调用parent::init()
。你的意思是让这个类是静态的,而不是抽象的吗?
答案 2 :(得分:1)
您构建代码的方式是不对的。 PHP确实允许许多疯狂的东西,但定义常量和输出代码作为抽象类的一部分在OOP方面没有任何意义。
您可能要做的是拥有一个帮助程序类,它根据本地服务器名称定义配置设置。为此,您有几个选择:
选项1)使用构造函数创建常规类。
class Config {
public $x;
public $y;
public function __construct() {
switch (...) {
$this->x = 2;
$this->y = 3;
}
}
}
并像这样使用它:
$config = new Config();
echo "Variable y: " . $config->y;
选项2)抽象类中的静态方法。
abstract class Config {
public $boolInitialized = false;
public static function init() {
if (self::$boolInitialized) {
return;
}
self::$boolInitialized = true;
switch (...) {
self::$x = 1;
self::$y = 2;
}
}
public static function getX() {
self::init();
return self::$x;
}
}
并像这样使用它:
echo Config::getX();
答案 3 :(得分:0)
你不能在没有声明第一个方法的情况下输出类中的内容 这将失败
abstract class fo
{
public $fo;
echo $fo;
}
**but this will work**
abstract class fo
{
public $fo;
public function __construct()
{
$this->fo = $_SERVER['SERVER_NAME'];
}
public function sayFo()
{
echo $this->fo;
}
}