Jump to content

OOP Constants


TLaude

Recommended Posts

Here is what I'm trying to accomplish.

 

I need a file (const_values.php) to store primary values that will allow me to use them all over my website while having the values originate in 1 location so if the value ever changes, I can just change it in the 1 spot.

 

Then, once const_values.php is set up, how would I go about calling it into my functions.php page?

 

Here is what I am thinking, but I'm not sure how well it is going to work.

 

// const_values.php
<?php

class const_values
{
  function const_values()
  {
    define(DB_HOST, '***');
    define(DB_USER, '***');
    define(DB_PASS, '***');
  }
}
?>

 

<?php
// functions.php

include('const_values.php')

var $dbhost;
var $dbuser;
var $dbpass;

$this->dbhost = const_values->DB_HOST;
$this->dbuser = const_values->DB_USER;
$this->dbpass = const_values->DB_PASS;

class database
{
  functions dbConnect()
  {
    mysql_connect($this->dbhost, $this->dbuser, $this->dbpass)
  }
}
?>

 

Am I close or am I far off? ;)

Link to comment
Share on other sites

class Configuration {
    const DB_HOST = 'host';
    const DB_USERNAME = 'username';
    const DB_PASSWORD = 'password';
    const DB_NAME = 'name';
}

class Database {
    protected $_host = null;
    protected $_username = null;
    protected $_password = null;
    protected $_name = null;
    
    public function __construct() {
        $this->_host = Configuration::DB_HOST;
        $this->_username = Configuration::DB_USERNAME;
        $this->_password = Configuration::DB_PASSWORD;
        $this->_name = Configuration::DB_NAME;
    }
}

Link to comment
Share on other sites

class Configuration {
    const DB_HOST = 'host';
    const DB_USERNAME = 'username';
    const DB_PASSWORD = 'password';
    const DB_NAME = 'name';
}

class Database {
    protected $_host = null;
    protected $_username = null;
    protected $_password = null;
    protected $_name = null;
    
    public function __construct() {
        $this->_host = Configuration::DB_HOST;
        $this->_username = Configuration::DB_USERNAME;
        $this->_password = Configuration::DB_PASSWORD;
        $this->_name = Configuration::DB_NAME;
    }
}

 

I need them in DIFFERENT files.

Link to comment
Share on other sites

class Configuration {
    const DB_HOST = 'host';
    const DB_USERNAME = 'username';
    const DB_PASSWORD = 'password';
    const DB_NAME = 'name';
}

class Database {
    protected $_host = null;
    protected $_username = null;
    protected $_password = null;
    protected $_name = null;
    
    public function __construct() {
        $this->_host = Configuration::DB_HOST;
        $this->_username = Configuration::DB_USERNAME;
        $this->_password = Configuration::DB_PASSWORD;
        $this->_name = Configuration::DB_NAME;
    }
}

 

I need them in DIFFERENT files.

 

No problemo. Create a file class.configuration.php and a file class.database.php. In your class.database.php add the line and in every other file you need the configuration constants:

 

require_once('class.configuration.php');

Link to comment
Share on other sites

the reason ignace's example would work is because you are trying to go into an instance, where as he is looking inside the class model, also you used define() which would go into the global scope rather then the object's scope, also.. You're defining it inside a function which requires a process to define.. which than isn't findable within the original class model.

 

:: looks into the original model of the class.. -> looks into an instance of that class

Link to comment
Share on other sites

no... just programmers seperate regular php files.. from class or library php files..

 

if you're making the php file JUST for a class.. for your own future ease.. you'd name it XXXX.class.php or class.xxxx.php or w.e

 

and some peopl ename it lib.xxx.php or xxx.lib.php for libraries of functions.. its just a programming method

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.