简体   繁体   English

类[DateTime('@-1')]的PHP对象实例在另一个类中为常量

[英]PHP object instance of class [DateTime('@-1')] as constant in another class

I am defining a class which has a DateTime object as one of the properties. 我正在定义一个具有DateTime对象作为属性之一的类。 I want to set its default value to unix timestamp of '-1'. 我想将其默认值设置为-1的unix时间戳。 I also want this default value to be constant so that all objects know its value. 我还希望此默认值是恒定值,以便所有对象都知道其值。

But I do not want to declare something like const PROPERTY_DEFAULT_DATE = '-1'; 但是我不想声明类似const PROPERTY_DEFAULT_DATE = '-1'; as the property will be a DateTime object and operations/functions using this default value and the property will be difficult to handle if PROPERTY_DEFAULT_DATE is not a proper DateTime object 因为该属性将是一个DateTime对象以及使用该默认值的操作/函数,并且如果PROPERTY_DEFAULT_DATE不是正确的DateTime对象,则该属性将难以处理

So, can I have particular object instance of a class as constant inside another class? 那么,我可以在另一个类中将某个类的特定对象实例作为常量吗?


The PHP manual says PHP手册说

The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call. 该值必须是一个常量表达式,而不是(例如)变量,属性,数学运算结果或函数调用。

Doesn't mention about this, but I think it can't be done (tried lot of variations, always got syntax errors) 没有提及,但是我认为这是不可能的(尝试了很多变化,总是会出现语法错误)
If it's not possible, what alternatives do I have? 如果不可能,我有什么选择?


Edit : I think I need to find the solution to the problem of defining a "Default Value" to my property, which in this case happens to be a DateTime object. 编辑:我想我需要找到对我的属性定义“默认值”的问题的解决方案,在这种情况下碰巧是一个DateTime对象。
What default value will you set in the __construct() ? 将在__construct()设置什么默认值? (no, not NULL please, I expect something more elegant solution must exist) Also keep in mind that it may be used in operations/functions inside the class/subclass (不,请不要为NULL,我希望必须存在更优雅的解决方案)也请记住,它可能会用于类/子类中的操作/函数中
Update : I followed the advice here and created a private $_DEFAULT_DATE property and a getter for it. 更新:我按照这里的建议,创建了一个private $_DEFAULT_DATE属性和一个吸气剂。 There is no setter for this property, so I can be assured that it'll not be changed. 此属性没有设置器,因此可以确信它不会被更改。 (Of course, I take care not to change it within class implementation) (当然,我注意不要在类实现中更改它)

Well, unfortunately, the manual is right. 好吧,不幸的是,该手册是正确的。 You cannot put an object in a constant. 您不能将对象放入常量。 You can make it a property, or in your case a static function might be suited; 您可以将其设置为属性,或者可以使用静态函数;

YourClass::getDefaultDate(); // return DateTime('-1');

It's not possible. 这是不可能的。 The simplest alternative is to use a static property, but it sounds like you want to make sure this property does not change. 最简单的选择是使用静态属性,但这听起来像您要确保此属性不变。

So in that case the only logical way to do this, is by making the static property private, and add a static function that returns the DateTime object. 因此,在这种情况下,唯一可行的方法是将静态属性设为私有,并添加一个返回DateTime对象的静态函数。

However, I still don't think you want to use a singular object. 但是,我仍然不希望您使用单个对象。 If any other method requests this default object they'll be able to modify it and you might get weird results. 如果有任何其他方法请求此默认对象,他们将能够对其进行修改,并且您可能会得到怪异的结果。 Any request to this method (in my mind) should receive a new or cloned DateTime object. 对这个方法的任何请求(在我看来)都应该收到一个新的或克隆的DateTime对象。

The manual is correct: no, you can't use objects in const expressions in PHP. 该手册是正确的:不,您不能在PHP的const表达式中使用对象。

You have to initialize a proper member inside a constructor if you want to use it this way. 如果要以这种方式使用它,则必须在构造函数中初始化一个适当的成员。 If you want it to be unalterable, with certain effort you can make it so . 如果您希望它不可更改, 那么您可以做出一些努力

from php.net about sonstant syntax and use Someone spoke about "dynamic" assignments to constants. 来自php.net的有关常量语法和使用的信息有人谈到了对常量的“动态”分配。 What? 什么? There are no dynamic assignments to constants, runtime assignments work only with variables. 没有对常量的动态分配,运行时分配仅适用于变量。 Let's take the proposed example: 让我们以建议的示例为例:

<?php 
/** 
 * Constants that deal only with the database 
 */ 
class DbConstant extends aClassConstant { 
    protected $host = 'localhost'; 
    protected $user = 'user'; 
    protected $password = 'pass'; 
    protected $database = 'db'; 
    protected $time; 
    function __construct() { 
        $this->time = time() + 1; // dynamic assignment 
    } 
} 
?> 

Those aren't constants, those are properties of the class. 这些不是常量,而是类的属性。 Something like "this->time = time()" would even totally defy the purpose of a constant. 诸如“ this-> time = time()”之类的东西甚至会完全违反常量的目的。 Constants are supposed to be just that, constant values, on every execution. 常量应该在每次执行时就是常量值。 They are not supposed to change every time a script runs or a class is instantiated. 不应在每次运行脚本或实例化类时更改它们。

Conclusion: Don't try to reinvent constants as variables. 结论:不要尝试将常量重新定义为变量。 If constants don't work, just use variables. 如果常量不起作用,请使用变量。 Then you don't need to reinvent methods to achieve things for what is already there. 然后,您不需要重新发明方法就可以实现已经存在的东西。

From self: you can use private static methods and use magic methods __getStatic (since it's avaliablr only from php 5.3) or use simple __get and property_exist or use Reflection. 从自身出发:您可以使用私有静态方法,也可以使用魔术方法__getStatic(因为仅从php 5.3起才有avaliablr),或者使用简单的__get和property_exist或使用Reflection。 But actually I don't see the problem which need this solution. 但是实际上我看不到需要此解决方案的问题。 Sorry (( 对不起((

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM