开发者

Php abstract class site configuration

开发者 https://www.devze.com 2022-12-13 01:44 出处:网络
i have a config class which is an abstract class. I would like to set it up so it automatically detects which server the site is on and then assigns appropriate constants. I get an error on line ten $

i have a config class which is an abstract class. I would like to set it up so it automatically detects which server the site is on and then assigns appropriate constants. I get an error on line ten $this->hostName = $_SERVER['SERVER_NAME']; expecting `T_FUNCTION. What is the correct way to do this and is there a better way to do this? Here is the first part of my class

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 开发者_如何学Go= "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;
    }


An abstract class shouldn't allow you to set private data (only an inherited concrete class).

Also, take a look at this link in stackoverflow for an interesting discussion on SERVER_NAME vs. HTTP_HOST


You have to wrap all that code in a constructor or better yet a function called init() or somthing. Then when you override init in the child classes you would call parent::init(). Did you mean to make this class static, not abstract?


The way you're structuring your code is just not right. PHP does allow for many crazy things, but defining constants and outputting code as a part of an abstract class makes no sense whatsoever in terms of OOP.

What you probably mean to do is to have a helper class that defines configuration settings based upon the local server name. To do so, you have a couple options:

Option 1) Create a regular class with a constructor.

class Config {
   public $x; 
   public $y;

   public function __construct() {
      switch (...) {
          $this->x = 2;
          $this->y = 3;
      }
   }
}

And use it like this:

$config = new Config();
echo "Variable y: " . $config->y;

Option 2) Static method in an abstract class.

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;
   }
}

And use it like this:

echo Config::getX();


You cant output something in a class without declaring first a method this will fail

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;
}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消