Question

I am defining a class which has a DateTime object as one of the properties. I want to set its default value to unix timestamp of '-1'. 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'; 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

So, can I have particular object instance of a class as constant inside another class?


The PHP manual says

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.
What default value will you set in the __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
Update : I followed the advice here and created a private $_DEFAULT_DATE property and a getter for it. 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)

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

The manual is correct: no, you can't use objects in const expressions in PHP.

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. 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. 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. But actually I don't see the problem which need this solution. Sorry ((

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top