views:

219

answers:

4

Hi, 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 = "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;
    }

Thankyou

+1  A: 

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?

Byron Whitlock
+2  A: 

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

Arthur Frankel
A: 

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

}
streetparade
A: 

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();
Alex
Sorry im new at this could you give an example with the abstract class where you switch the constants based on the host.
andrew