I'm wondering if PHP has a type of variable in classes that functions like static in other languages. And by that I mean all objects of the same class use the same variable and when it's updated on one it's updated on every one. Static is close because it is shared throughout all objects but I need to be able to update it. Will I have to use globals for this?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I don't see why making the variable static doesn't work for what you described (but it has nothing to do with the keyword final)?
I think static is what you want. You can update a static variable, you just have to do it in a "static context" (ie. using the :: operator.
will output:
int 0
int 1
The correct answer is that there is no equivalent in PHP to final, but static seems like what you wanted in the first place anyway.
static has the property that it will have the same value across all instances of a class, because it is not tied to a particular instance.
You will need to use the :: operator to access it, because being static, you cannot use ->.
You can simply create variables in a PHP file say named Constants.
--Constants.php-- $DATABASE_NAME = "mysql"
and include the file in your file. You can change its value. It comes close to what you want, but it is not good call them constants because constants aren't meant to be changed, that's what confused me :).
I think
static
is the keyword you are looking for.And there is nothing that prevents a
static
property from beeing "updated", in PHP : it is initialized the first time it's set, it keeps it value during the execution of the PHP script, but you definitly can set it to a new value.You can update static properties: