Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PHPGoto page 1, 2, 3  Next

Including config variables in OOP

 
Jump to:  
 
FutureShock
PostPosted: Sun Aug 31, 2008 8:23 pm    Post subject: 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
 

 
Jerry Stuckle
PostPosted: Sun Aug 31, 2008 11:10 pm    Post subject: Re: Including config variables in OOP
       
FutureShock wrote:
Quote:
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.
jstucklex@attglobal.net
==================
 

 
Christopher Vogt
PostPosted: Mon Sep 01, 2008 12:08 am    Post subject: Re: Including config variables in OOP
       
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
 

 
FutureShock
PostPosted: Mon Sep 01, 2008 1:29 am    Post subject: Re: Including config variables in OOP
       
Jerry Stuckle wrote:
Quote:
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
 

 
FutureShock
PostPosted: Mon Sep 01, 2008 1:38 am    Post subject: Re: Including config variables in OOP
       
Christopher Vogt wrote:
Quote:
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
 

 
Christopher Vogt
PostPosted: Mon Sep 01, 2008 3:13 am    Post subject: Re: Including config variables in OOP
       
Quote:
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.

Quote:
Thanks for taking the time with your help

You're welcome Smile.

Christopher
 

 
Curtis
PostPosted: Mon Sep 01, 2008 5:14 am    Post subject: Re: Including config variables in OOP
       
Christopher Vogt wrote:
Quote:
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
 

 
Ulf Kadner
PostPosted: Mon Sep 01, 2008 6:11 am    Post subject: Re: Including config variables in OOP
       
FutureShock wrote:

Quote:
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.

Quote:
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
 

 
FutureShock
PostPosted: Mon Sep 01, 2008 12:55 pm    Post subject: Re: Including config variables in OOP
       
Christopher Vogt wrote:
Quote:
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 Smile.

Christopher
Exactly. I placed them in at the top prior to my Class definition so

now all methods have access to them.

Scotty
 

 
FutureShock
PostPosted: Mon Sep 01, 2008 12:57 pm    Post subject: Re: Including config variables in OOP
       
Curtis wrote:
Quote:
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
 

Page 1 of 3 .:. Goto page 1, 2, 3  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

kupię mieszkanie rowery apteka wózki widłowe zabawki