php中的类的var [英] var for a class in PHP

查看:59
本文介绍了php中的类的var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP类进行登录.登录过程为用户检查的MySQL数据库的定义如下:

I am using a PHP class for login purposes. The MySQL database that the login process checks for the user is defined like this:

    class flexibleAccess{

  var $dbName = 'mydatabase';

但是,当我为其他人安装应用程序时,此$ dbName需要不断更改.我决定制作一个配置文件来保存我的数据库信息,因此我将不得不在该处进行更改,因为此登录类被隐藏"在某处.问题是我想做这样的事情:

However, as I install the application for different people, this $dbName needs constant changes. I have decided to make a config file where I keep my database information so I would have to change there witch is more easy as this login class is "hidden" somewhere. The problem is I want to do something like this:

class flexibleAccess{

      var $dbName = $_SESSION['mydatabase'];

我得到了错误:解析错误:在第43行的 path /access.class.php中解析错误" 第43行是带有$ dbName的行...为什么不能使用它从会话中动态获取我的值?以及我应该如何使用它?

And I get the error: "Parse error: parse error in path/access.class.php on line 43" The line 43 is the line with $dbName... Why can't I use this to dynamically get my values from the session ? And how should I use it ?

谢谢

推荐答案

Do it in a constructor (I'm assuming php5 here...)

class flexibleAccess{

   private $dbName;

   function __construct() {
         $this->dbName = $_SESSION['mydatabase'];
   }
}

对于php4,将__construct替换为类的名称.

For php4, replace __construct with the name of the class instead.

这篇关于php中的类的var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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