By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

Including config variables in OOP

P: n/a
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
Share this Question
Share on Google+
21 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
..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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
..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

P: n/a
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

P: n/a
..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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.