在OOP中包含配置变量 [英] Including config variables in OOP

查看:79
本文介绍了在OOP中包含配置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在我的网络应用程序中使用OOP,而且我遇到了一些棘手的问题。


我想要一个单独的我所有配置变量的文件。

其中一些是为数据库操作重新映射表名。


$ session_tbl =''sessions'';

$ users_tbl =''用户'';

等...


现在在我的班级定义中我试图找到一种方法在构造中包含这个

文件,我得到通知,当需要它们时,它们没有被定义,并且它会破坏代码。


我尝试过使用公钥。


public $ session_tbl =''sessions'';

public $ users_tbl =' '用户'';


我得到了:

解析错误:语法错误,意外T_PUBLIC


当我在方法中声明一个公共var时,我通常会得到它。

所以这不起作用。


我已经使它工作了但是在每个需要配置数据的方法中,在

中放置一个单独的include语句。


但是我有一些方法需要文件然后它

调用另一个需要文件的方法所以我得到很多包括调用

在一个脚本中活动。


例如:


class auth(){


公共函数方法 - 1(){

包含file1

*代码*

}


公共函数方法 - 2(){

包括file1

*代码*

$ this-> method-1();

}

}


我不确定这是一个更好的方法,所以任何帮助都将是

赞赏。我可能会说错了。


谢谢

Scotty

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

推荐答案

session_tbl =''sessions'';
session_tbl = ''sessions'';


users_tbl =''users'';

等...


现在在我的类定义中,我试图找到一种方法在构造中包含这个

文件,并且我注意到它们在
它们是必需的并且它会破坏代码。


我尝试过使用公钥。


public
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
session_tbl = ''sessions'';
public


这篇关于在OOP中包含配置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆