PHP OOP 属性常量用法

发布于 2024-09-02 03:29:18 字数 325 浏览 4 评论 0原文

我对 OOP 真的很陌生。我什至不是新手——我是菜鸟。所以。我想将我的伪 CMS 从“正常”编程转移到 OOP 编程系统中。那么:

private static $dsn = DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME;

这里是什么导致了问题?约束的使用?我不知道。我的编辑器(aptana studio)在 1 个常量后显示错误。谢谢

编辑:

感谢您的快速反应。我会在构造函数中完成它。

Edit2:

但是如果我想使用单例怎么办?如何将参数传递给构造函数?

I'm really new to OOP. I'm even not a newbie - I'm noob. So. I want to transfer my pseudo-CMS from "normal" programming, into OOP programming system. And so:

private static $dsn = DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME;

What here causes problem? Usage of constraints? I don't know. My editor (aptana studio) shows error after 1 constant. Thanks

Edit:

Thanks for fast reaction. I'll do it in contructor.

Edit2:

But what if i want to use singleton? How to pass arguments to contructor?

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

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

发布评论

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

评论(3

赠佳期 2024-09-09 03:29:18

所有变量声明必须是完全静态的。这意味着不使用常量、变量或其他可变项。

要制作任何不完全纯文本的内容,您应该使用构造函数。

All variable declarations must be completely static. This means not use of constants, variables, or other changeable items.

To make anything that is not completely plain text, you should use the constructor.

寒江雪… 2024-09-09 03:29:18

问题在于,当您将属性放入初始值设定项字段时,属性必须是内联常量。

你正在做的事情不会起作用,但是,例如,这会:

private static $dsn = 'mysql:host=localhost;dbname=mydb';

我知道,这很愚蠢,但是你甚至不能使用 PHP 常量。您必须以纯文本形式获取它。

解决方案是在类的构造函数中初始化 $dsn,如下所示:

class MyClass
{
    public function __construct()
    {
        self:: $dsn = DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME;
    }
}

The problem is that properties have to be inline constants when you put them in the initializer fields.

What you're doing won't work, but this, for example, would:

private static $dsn = 'mysql:host=localhost;dbname=mydb';

I know, it's stupid, but you can't even use PHP constants. You have to literally have it in plain text.

The solution to this is to initialize $dsn in the class's constructor, like this:

class MyClass
{
    public function __construct()
    {
        self:: $dsn = DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME;
    }
}
凉薄对峙 2024-09-09 03:29:18

请参阅文档

此声明可以包含初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时求值,并且不能依赖于运行时信息才能求值。

定义类属性时不能连接字符串。

文档中的示例(为了完整性):

<?php
class SimpleClass
{
   // invalid property declarations:
   public $var1 = 'hello ' . 'world';
   public $var2 = <<<EOD
hello world
EOD;
   public $var3 = 1+2;
   public $var4 = self::myStaticMethod();
   public $var5 = $myVar;

   // valid property declarations:
   public $var6 = myConstant;
   public $var7 = array(true, false);

   // This is allowed only in PHP 5.3.0 and later.
   public $var8 = <<<'EOD'
hello world
EOD;
}

?>

无论如何,当您切换到 OOP 时,您不应该使用常量。将这些值传递给类的构造函数:

public function __construct($db_type, $db_host, $db_name) {
    self::$dsn = $db_type.':host='.$db_host.';dbname='.$db_name;
}

See the documentation:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

You cannot concatenate strings when defining class properties.

Example from the documentation (for completness):

<?php
class SimpleClass
{
   // invalid property declarations:
   public $var1 = 'hello ' . 'world';
   public $var2 = <<<EOD
hello world
EOD;
   public $var3 = 1+2;
   public $var4 = self::myStaticMethod();
   public $var5 = $myVar;

   // valid property declarations:
   public $var6 = myConstant;
   public $var7 = array(true, false);

   // This is allowed only in PHP 5.3.0 and later.
   public $var8 = <<<'EOD'
hello world
EOD;
}

?>

As you switch to OOP anyway you should not use constants. Pass those values to the constructor of your class:

public function __construct($db_type, $db_host, $db_name) {
    self::$dsn = $db_type.':host='.$db_host.';dbname='.$db_name;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文