473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Including config variables in OOP

I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include calls
in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}
}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty

Aug 31 '08 #1
21 2543
FutureShock wrote:
I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include calls
in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}
}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty

If it's needed by multiple methods in the class, it should be a member
of the class.

I think you need to rethink your entire approach. Things like this
which are pertinent to a single class I put directly in the class
definition. If it's needed by multiple classes, perhaps you need a
configuration class which contains the information.

But the way you're doing it is not good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 1 '08 #2
You want to define the configuration in a central file, that you can
replace easily. And you need to be able to access the configuration from
everywhere. So you need something with global access. There are several
options: Constants, global variables or a class implementing the
Registry pattern. If you are unsure read about the options ind the PHP
manual and google.

Then define the configuration in the configuration file and include it
once, e.g. at the beginning of your file defining the class. Example:

config.inc.php:
define( 'SESSION_TABLE', 'sessions' );
define( 'USER_TABLE', 'sessions' );

SomeClass.php:
require 'config.inc.php';
class SomeClass{
function doSomething(){
echo USER_TABLE;
}
}
Chris
Sep 1 '08 #3
Jerry Stuckle wrote:
FutureShock wrote:
>I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include
calls in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}
}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty


If it's needed by multiple methods in the class, it should be a member
of the class.

I think you need to rethink your entire approach. Things like this
which are pertinent to a single class I put directly in the class
definition. If it's needed by multiple classes, perhaps you need a
configuration class which contains the information.

But the way you're doing it is not good.
OK that makes sense.

Thanks

Scotty
Sep 1 '08 #4
Christopher Vogt wrote:
You want to define the configuration in a central file, that you can
replace easily. And you need to be able to access the configuration from
everywhere. So you need something with global access. There are several
options: Constants, global variables or a class implementing the
Registry pattern. If you are unsure read about the options ind the PHP
manual and google.

Then define the configuration in the configuration file and include it
once, e.g. at the beginning of your file defining the class. Example:

config.inc.php:
define( 'SESSION_TABLE', 'sessions' );
define( 'USER_TABLE', 'sessions' );

SomeClass.php:
require 'config.inc.php';
class SomeClass{
function doSomething(){
echo USER_TABLE;
}
}
Chris
Using constants worked perfectly. I thought I had tried that before with
many notices, but I probably included it in the wrong part of the
hierarchy of the class.

Thanks for taking the time with your help

Scotty
Sep 1 '08 #5
Using constants worked perfectly. I thought I had tried that before with
many notices, but I probably included it in the wrong part of the
hierarchy of the class.
Constants can only be defined once. That might have lead to the notices,
in case you included the file defining them multiple times. But since
they are absolutely global you only need to include them once.
Thanks for taking the time with your help
You're welcome :).

Christopher
Sep 1 '08 #6
Christopher Vogt wrote:
You want to define the configuration in a central file, that you can
replace easily. And you need to be able to access the configuration from
everywhere. So you need something with global access. There are several
options: Constants, global variables or a class implementing the
Registry pattern. If you are unsure read about the options ind the PHP
manual and google.

Then define the configuration in the configuration file and include it
once, e.g. at the beginning of your file defining the class. Example:

config.inc.php:
define( 'SESSION_TABLE', 'sessions' );
define( 'USER_TABLE', 'sessions' );

SomeClass.php:
require 'config.inc.php';
class SomeClass{
function doSomething(){
echo USER_TABLE;
}
}
This may lead to some notices, as you can end up defining constants
after they've already been defined. You need to use the require_once
construct, rather than require. It may not be an issue when the
structure is simple, however, if/when the app scales in complexity,
this issue may very well arise, which could possibly lead to (a)
bug(s) of an unpredictable nature.

--
Curtis
Sep 1 '08 #7
FutureShock wrote:
I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';
If you define vars in included file like this:

$this->session_tbl = 'sessions';
$this->users_tbl = 'users';
# ...

and include it within the class like:

class Abc
{
public $session_tbl;
public $users_tbl;
public function __construct()
{
include 'myconfig.php';
}
}

it works fine. But its for real not the prefered way to work with
userfriendly accessible config data.

A Better way ist to work with some userfriendly config files like
INI-Files (php brings own functions to handle inifiles) or XML-Konfigs
(maybe typed). XML works fine with SimpleXML in most cases.
class auth() {

public function method-1() {
WTF! A PHP-word can only contain characters from range
^[a-zA-Z_][a-zA-Z0-9_]*$

method-* isnt a vilid PHP-word!

Ulf
Sep 1 '08 #8
On 31 Aug, 23:23, FutureShock <futuresh...@att.netwrote:
I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include calls
in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}

}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty
include (and require) import symbols into global scope regardless of
where they are called from (but can still return). It'd be really neat
if code like that above imported into the CLASS scope - instant
namespaces! But PHP doesn't work that way.

Considered using a dedicated config file parser? This also avoids the
problem of reconfig resulting in code changes.

C.
Sep 1 '08 #9
Christopher Vogt wrote:
>Using constants worked perfectly. I thought I had tried that before with
many notices, but I probably included it in the wrong part of the
hierarchy of the class.

Constants can only be defined once. That might have lead to the notices,
in case you included the file defining them multiple times. But since
they are absolutely global you only need to include them once.
>Thanks for taking the time with your help

You're welcome :).

Christopher
Exactly. I placed them in at the top prior to my Class definition so
now all methods have access to them.

Scotty
Sep 1 '08 #10
Curtis wrote:
Christopher Vogt wrote:
>You want to define the configuration in a central file, that you can
replace easily. And you need to be able to access the configuration from
everywhere. So you need something with global access. There are several
options: Constants, global variables or a class implementing the
Registry pattern. If you are unsure read about the options ind the PHP
manual and google.

Then define the configuration in the configuration file and include it
once, e.g. at the beginning of your file defining the class. Example:

config.inc.php:
define( 'SESSION_TABLE', 'sessions' );
define( 'USER_TABLE', 'sessions' );

SomeClass.php:
require 'config.inc.php';
class SomeClass{
function doSomething(){
echo USER_TABLE;
}
}

This may lead to some notices, as you can end up defining constants
after they've already been defined. You need to use the require_once
construct, rather than require. It may not be an issue when the
structure is simple, however, if/when the app scales in complexity, this
issue may very well arise, which could possibly lead to (a) bug(s) of an
unpredictable nature.

--
Curtis
You mean if I have more then one instance of the class created per
script? That would throw a Notice. I will use require_once and wring it
out to see if that protects it.
Thanks

Scotty
Sep 1 '08 #11
Ulf Kadner wrote:
FutureShock wrote:
>I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

If you define vars in included file like this:

$this->session_tbl = 'sessions';
$this->users_tbl = 'users';
# ...

and include it within the class like:

class Abc
{
public $session_tbl;
public $users_tbl;
public function __construct()
{
include 'myconfig.php';
}
}

it works fine. But its for real not the prefered way to work with
userfriendly accessible config data.

A Better way ist to work with some userfriendly config files like
INI-Files (php brings own functions to handle inifiles) or XML-Konfigs
(maybe typed). XML works fine with SimpleXML in most cases.
>class auth() {

public function method-1() {

WTF! A PHP-word can only contain characters from range
^[a-zA-Z_][a-zA-Z0-9_]*$

method-* isnt a vilid PHP-word!

Ulf
TITF! This is probably not my actual script since I did not mention an
error pointing it out. I merely used it as a way to show my hierarchy,
sorry for the confusion.

I will look at the INI option, that will be new to me but I am curious
to learn how to use it.

Thanks
Scotty
Sep 1 '08 #12
C. (http://symcbean.blogspot.com/) wrote:
On 31 Aug, 23:23, FutureShock <futuresh...@att.netwrote:
>I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include calls
in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}

}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty

include (and require) import symbols into global scope regardless of
where they are called from (but can still return). It'd be really neat
if code like that above imported into the CLASS scope - instant
namespaces! But PHP doesn't work that way.

Considered using a dedicated config file parser? This also avoids the
problem of reconfig resulting in code changes.

C.
Config file parser. Is there one of these animals out there or would I
need to write my own?

Thanks
Scotty
Sep 1 '08 #13
..oO(FutureShock)
>I will look at the INI option, that will be new to me but I am curious
to learn how to use it.
I also use some INI files here (one for the common settings, one for DB
stuff, etc.), which works very well. Have a look at parse_ini_file().
Especially its second parameter can make things very easy & convenient.

http://www.php.net/parse_ini_file

Micha
Sep 1 '08 #14
Michael Fesser wrote:
.oO(FutureShock)
>I will look at the INI option, that will be new to me but I am curious
to learn how to use it.

I also use some INI files here (one for the common settings, one for DB
stuff, etc.), which works very well. Have a look at parse_ini_file().
Especially its second parameter can make things very easy & convenient.

http://www.php.net/parse_ini_file

Micha
OK now I am wringing my hands in anticipation of using that in my
applications.
I can see the $process_section will allow you to compartmentalize your
configuration setting.

Plus I am finding out that PHP will not parse constants in a string and
that you have to concatenate them with the '.'. So this may help in that
area.

Thanks very much for that find.

Scotty
Sep 1 '08 #15
FutureShock wrote:
I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include calls
in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}
}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty
Scotty,

I use a class specifically set up to parse a config file, that class is
then set up as a singleton so it can be set up as a member in any object

class PHP_Config
{
private $config;
private static $instance = null;

//Singletons should have private constructors
private function __construct(){
//Parse config file here
}

//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}
}

//Later in your code
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

My config file then is just a regular php file with one variable per line:

$sqlserver="localhost";
$sqluser="sqluser";
....

And I use fgets() to parse line by line and put the vars into
$config->config and use __get() so that I can get them by name:

$config->sqlserver;

Hope it helps.

Isaac
Sep 1 '08 #16
King Isaac wrote:
FutureShock wrote:
>I have just recently started to use OOP for my web applications and am
running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined when
they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement in
each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include
calls in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}
}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty

Scotty,

I use a class specifically set up to parse a config file, that class is
then set up as a singleton so it can be set up as a member in any object

class PHP_Config
{
private $config;
private static $instance = null;

//Singletons should have private constructors
private function __construct(){
//Parse config file here
}

//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}
}

//Later in your code
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

My config file then is just a regular php file with one variable per line:

$sqlserver="localhost";
$sqluser="sqluser";
...

And I use fgets() to parse line by line and put the vars into
$config->config and use __get() so that I can get them by name:

$config->sqlserver;

Hope it helps.

Isaac
I am sure it will. I am not familiar with some of the code you wrote
but let me try to get my head around it:

In:
//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}

When it is called, creates an instance of its class (PHP_Config), but
only once no matter how many times it is called, right?

Now this is where I get lost:
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

Now from what I can tell it is calling the GetInstance() method which is
creating an instance of itself (PHP_Config) which is parsing the config
file.
But the '$this->config' throws me. I thought that this was how you
access a member of the class it is called from, but it looks like it is
really accessing the member of the PHP_Config class.
It may be that I just have much more to learn about OOP, but any extra
explanation would be appreciated.

Thanks for your time and effort.
Off to PHP.net.

Scotty

Sep 2 '08 #17
FutureShock wrote:
King Isaac wrote:
>FutureShock wrote:
>>I have just recently started to use OOP for my web applications and
am running into some head scratching issues.

I wanted to have a separate file for all my configuration variables.
Some of them being remapping table names for DB operations.

$session_tbl = 'sessions';
$users_tbl = 'users';
etc...

Now in my Class definition I was trying to find a way to include this
file in the construct and I get notices that they are not defined
when they are needed and it breaks the code.

I've tried using the public key word.

public $session_tbl = 'sessions';
public $users_tbl = 'users';

And I get a:
Parse error: syntax error, unexpected T_PUBLIC

Which I usually get when I declare a public var inside a method.
So that did not work.

I have made it work however by placing a separate include statement
in each method that needs the configuration data.

But I have a few situations where a method needs the file and then it
calls another method that needs the file so I get numerous include
calls in a single script activity.

for example:

class auth() {

public function method-1() {
include file1
*code*
}

public function method-2() {
include file1
*code*
$this->method-1();
}
}

I am not sure of a better way of doing this, so any help would be
appreciated. I may be going about it all wrong.

Thanks
Scotty

Scotty,

I use a class specifically set up to parse a config file, that class
is then set up as a singleton so it can be set up as a member in any
object

class PHP_Config
{
private $config;
private static $instance = null;

//Singletons should have private constructors
private function __construct(){
//Parse config file here
}

//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}
}

//Later in your code
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

My config file then is just a regular php file with one variable per
line:

$sqlserver="localhost";
$sqluser="sqluser";
...

And I use fgets() to parse line by line and put the vars into
$config->config and use __get() so that I can get them by name:

$config->sqlserver;

Hope it helps.

Isaac

I am sure it will. I am not familiar with some of the code you wrote
but let me try to get my head around it:

In:
//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}

When it is called, creates an instance of its class (PHP_Config), but
only once no matter how many times it is called, right?

Now this is where I get lost:
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

Now from what I can tell it is calling the GetInstance() method which is
creating an instance of itself (PHP_Config) which is parsing the config
file.
But the '$this->config' throws me. I thought that this was how you
access a member of the class it is called from, but it looks like it is
really accessing the member of the PHP_Config class.
It may be that I just have much more to learn about OOP, but any extra
explanation would be appreciated.

Thanks for your time and effort.
Off to PHP.net.

Scotty

I do something similar, but I normally store configuration values in a
MySQL table. 3 columns - the identifier, the value, and a description
of what it's for.

I find this to be eminently more flexible than storing things in a file
- ini or otherwise.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 2 '08 #18
..oO(FutureShock)
>I am sure it will. I am not familiar with some of the code you wrote
but let me try to get my head around it:

In:
//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}

When it is called, creates an instance of its class (PHP_Config), but
only once no matter how many times it is called, right?
Correct. The above is called a singleton. It ensures that there will
always be only one instance of the class.

Using the magic __CLASS__ constant is not really necessary there, the
following will work as well. Additionally the method has to be marked as
'static' or PHP will throw an E_STRICT error:

public static function getInstance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
>Now this is where I get lost:
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

Now from what I can tell it is calling the GetInstance() method which is
creating an instance of itself (PHP_Config) which is parsing the config
file.
Correct. It creates a new PHP_Config instance if it doesn't exist yet,
and returns it to the caller.
>But the '$this->config' throws me.
$config is a member of the class Generic_Object, even though it would be
better to declare it first:

class Generic_Object {
protected $config;

public function __construct() {
$this->config = PHP_Config::getInstance();
}
}
>I thought that this was how you
access a member of the class it is called from, but it looks like it is
really accessing the member of the PHP_Config class.
Nope. It belongs to the Generic_Object that's about to be instantiated.
The constructor just gets the configuration object and assigns it to its
own $config property, so that other methods can easily access it.

Micha
Sep 2 '08 #19
Michael Fesser wrote:
.oO(FutureShock)
>I am sure it will. I am not familiar with some of the code you wrote
but let me try to get my head around it:

In:
//Provide method to access single instance
public function GetInstance(){
if(is_null(self::$instance)){
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}

When it is called, creates an instance of its class (PHP_Config), but
only once no matter how many times it is called, right?

Correct. The above is called a singleton. It ensures that there will
always be only one instance of the class.

Using the magic __CLASS__ constant is not really necessary there, the
following will work as well. Additionally the method has to be marked as
'static' or PHP will throw an E_STRICT error:

public static function getInstance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
>Now this is where I get lost:
class Generic_Object
{
public function __construct(){
$this->config = PHP_Config::GetInstance();
}
}

Now from what I can tell it is calling the GetInstance() method which is
creating an instance of itself (PHP_Config) which is parsing the config
file.

Correct. It creates a new PHP_Config instance if it doesn't exist yet,
and returns it to the caller.
>But the '$this->config' throws me.

$config is a member of the class Generic_Object, even though it would be
better to declare it first:

class Generic_Object {
protected $config;

public function __construct() {
$this->config = PHP_Config::getInstance();
}
}
>I thought that this was how you
access a member of the class it is called from, but it looks like it is
really accessing the member of the PHP_Config class.

Nope. It belongs to the Generic_Object that's about to be instantiated.
The constructor just gets the configuration object and assigns it to its
own $config property, so that other methods can easily access it.

Micha
OK Now I get it. So $config of $this->config IS a member of
Gerneric_Object. All the pieces fall into place.

Again thanks for taking the time to explain that to me. Learn something
new everyday.

Scotty
Sep 2 '08 #20
..oO(FutureShock)
>Again thanks for taking the time to explain that to me. Learn something
new everyday.
You're welcome.

Micha
Sep 2 '08 #21
OK Sorry to bring this back up again but I got it to work with a lil
studying. I would like to ask if this looks like a right (practical) way
to do this.

Here is my complete code:

class PHP_Config
{
private $config;
private static $instance = NULL;

private function __construct() {

** I used a INI formatted file
** example:
** host = localhost
** dbname = blabla

$this->config = parse_ini_file($config_file);
}

public function GetInstance() {
if(is_null(self::$instance)) {
$cname = __CLASS__;
self::$instance = new $cname;
}
return self::$instance;
}

*** This part I had to figure out, hope its right...no error checks yet.

public function __get($name) {
return $this->config[$name];
}
}

class test
{
public $new_config;
public function __construct() {
$this->new_config = PHP_Config::GetInstance();
}

**** Just used a simple method to ensure it works

public function displayHost() {
echo $this->new_config->host."<br>";
}
}

Call the method:
$test = new test();
$test->displayHost();

OUPUT: localhost
Thanks
Scotty
Sep 3 '08 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: will taubin | last post by:
i would like my program to have a config.php with passwords and other stuff. i would like to have a functions.php to hold all my functions. i would like the functions.php to include/require...
1
by: Arne Lund | last post by:
I've to read variables out of a config-file, that is stored on a Linux-machine. When I connect to the server with puTTY, I can execute the config with: .. /usr/local/webspace/config then I can...
13
by: gaudetteje | last post by:
Hi All, I've been trying to come up with an elegant solution to this problem, but can't seem to think of anything better than my solution below. I have a Python program that needs to be...
6
by: Klaus Jensen | last post by:
Hi! I have a pretty traditional setup where I develop on my local PC and the use "Copy Project" to deploy to the production enviroment.. In web.config I need different values for...
2
by: John Wildes | last post by:
hello I was wondering if someone could point me in the direction of information on using app.config to store string variables. I have a couple of variables that store path information for file...
2
by: Keith Elder | last post by:
Let's say you have a stand alone C# library project that is your datalayer. When this library compiles it will produce "My.DataLayer.dll" for example. In the project you use all the new...
4
by: Srini | last post by:
I noticed that ASP.Net caches the web.config and does not reread it unless it is changed. But if you change it, it restarts the app and even session variables are cleared (Am I correct ?). I used...
8
by: rottmanj | last post by:
. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications. One area where things are kind of fuzzy is setting up global variables that can be...
2
by: Fresno Bob | last post by:
I quite like to store variables in the web.config file. However it can make the web.config messy and make deployment fiddly. Is there any way of have multiple web.config files - one with the major...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.