PHP 中类的 var

发布于 2024-07-11 20:53:11 字数 483 浏览 6 评论 0原文

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

    class flexibleAccess{

  var $dbName = 'mydatabase';

但是,当我为不同的人安装应用程序时,这个 $dbName 需要不断更改。 我决定创建一个配置文件来保存数据库信息,这样我就必须在那里进行更改,因为这个登录类“隐藏”在某个地方,所以更容易。 问题是我想做这样的事情:

class flexibleAccess{

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

我收到错误:“解析错误:第 43 行 path/access.class.php 中的解析错误” 第 43 行是 $dbName 行...为什么我不能使用它从会话中动态获取我的值? 我应该如何使用它?

谢谢

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';

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'];

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 ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我三岁 2024-07-18 20:53:11

您可能不应该将数据库配置放在会话中。 对我来说感觉有点冒险。

我将使用这些信息创建一个配置文件,并将其包含在常量中:

dbconfig.php:

constant("DBNAME", 'mydatabase');

flexibleaccess.php:

require_once 'dbconfig.php';

class flexibleAccess {
  private $dbname;

  // set the default database name to the constant DBNAME
  // but allow override
  public function __construct ( $dbname = DBNAME )
  {
    $this->dbname = $dbname;
  }
}

任何需要数据库访问的文件:

require_once 'flexibleaccess.php';

$db = new flexibleaccess();

当然,您不需要将其设置为常量。 关键是您将配置与代码放在单独的文件中。

You probably shouldn't put database configuration in the session. It feels a bit risky to me.

I would create a configuration file with the information and include it with a constant:

dbconfig.php:

constant("DBNAME", 'mydatabase');

flexibleaccess.php:

require_once 'dbconfig.php';

class flexibleAccess {
  private $dbname;

  // set the default database name to the constant DBNAME
  // but allow override
  public function __construct ( $dbname = DBNAME )
  {
    $this->dbname = $dbname;
  }
}

Any files that need database access:

require_once 'flexibleaccess.php';

$db = new flexibleaccess();

Of course, you don't need ot set it as a constant. The key is that you're putting the configuration in a separate file from your code.

匿名。 2024-07-18 20:53:11

构造函数 中执行此操作(我在这里假设 php5 ...)

class flexibleAccess{

   private $dbName;

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

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

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

class flexibleAccess{

   private $dbName;

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

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

旧伤还要旧人安 2024-07-18 20:53:11

您必须在类的构造函数中将此变量分配为 PHP 4 仅允许常量作为初始值设定项的值

在 PHP 4 中,仅允许 var 变量的常量初始值设定项。 要使用非常量值初始化变量,您需要一个初始化函数,该函数在从类构造对象时自动调用。 这样的函数称为构造函数(见下文)。

You have to assign this variable in the constructor of the class as PHP 4 only allows constant values as initializers:

In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).

愿与i 2024-07-18 20:53:11

或者,您可以在调用类时指定类应从何处读取此信息,例如:

<?php
$login = new flexibleAccess();
$login->dbName = $_SESSION['mydatabase'];
?>

这样您的类就保持干净,并且可以从您喜欢的任何源或方法接受数据库名称(以及可能不同的任何其他信息) 。 作为一个额外的好处,您不必注意您使用的是哪个版本的 PHP,它在这两个版本中都可以正常工作。

希望这可以帮助!

-戴夫

Alternatively you can specify where the class should read this information from while calling the class, for example:

<?php
$login = new flexibleAccess();
$login->dbName = $_SESSION['mydatabase'];
?>

This way your class stays clean, and it can accept the database name (and any other information that might vary) from any source or method you prefer. As an added bonus, you don't have to pay attention to which version of PHP you're using, it'll work fine in both.

Hope this helps!

-Dave

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文