If I have a class structure with a value that can either be true or false, that doesn't change, currently implemented as variables would it be better to change them to constants, such as:
class Parent {
const BOOL_CONST = false;
...
}
class SomeChild extends Parent {
const BOOL_CONST = true;
...
}
Later I have an object which may be of any type in that class hierarchy, either the parent or one of its children, and some of the children may, like 'SomeChild' have overloaded the value to be true.
Is there some way I can access the constant without knowing the class? In other words can I do something like:
$object->BOOL_CONST
Or would it be better to leave these values as variables, even though they really shouldn't change?
UPDATE
I've reworded my question above to better express what I was attempting to ask.
Is there some way I can access the constant without knowing the class?
In other words can I do something like:
Yes, in order to reference a constant, you will want to use the following constructs:
self::NAME_OF_CONSTANT: give me a constant defined in this class; if I don't define it, get it from my parent
static::NAME_OF_CONSTANT: give me a constant defined in this class ONLY; never look to my parent for it
parent::NAME_OF_CONSTANT: give me a constant defined in my parent class ONLY; never look to myself for it
BTW, you used the term "overloaded"; however, I believe you meant to say "overridden". Overloading has a different semantic meaning in object oriented languages.
Constant as access with the double colon ::
Parent::BOOL_CONST
SomeChild::BOOL_CONST
within the class
parent::BOOL_CONST
self::BOOL_CONST
PHP 5.3 now accepts the object as the class reference: $this::BOOL_CONST is now accepted.
//
// http://php.net/manual/en/language.oop5.constants.php
//
// As of PHP 5.3.0, it's possible to
// reference the class using a variable.
// The variable's value can not be a keyword
// (e.g. self, parent and static).
//
// I renamed "Parent" class name to "constantes"
// because the classname "Parent" can be confused with "parent::" scope
class constantes
{
const test = false;
}
// I renamed "SomeChild" too, with no reason...
class OverloadConst extends constantes
{
const test = true;
public function waysToGetTheConstant()
{
var_dump(array('$this'=>$this::test)); // true, also usable outside the class
var_dump(array('self::'=>self::test)); // true, only usable inside the class
var_dump(array('parent::'=>parent::test)); // false, only usable inside the class
var_dump(array('static::'=>static::test)); // true, should be in class's static methods, see http://php.net/manual/en/language.oop5.late-static-bindings.php
}
}
// Classic way: use the class name
var_dump(array('Using classname' => OverloadConst::test));
// PHP 5.3 way: use the object
$object = new OverloadConst();
var_dump(array('Using object' => $object::test));
$object->waysToGetTheConstant();
Note that you can override a class constant, but not an interface constant.
If constantes is an interface that OverloadConsts implements, then you can not override its const test (or BOOL_CONST).
Sources
Constants in PHP 5.3: http://php.net/manual/en/language.oop5.constants.php
Late static Binding: http://php.net/manual/en/language.oop5.late-static-bindings.php
No, you can't access constants from an object context, but you could use reflection to grab the class of $object and then use :: to get BOOL_CONST. So:
$class = get_class($object);
$class::BOOL_CONST;
Okay, no, that's not technically reflection. Also, I'm not 100% sure if $class:: will resolve correctly. Use the actual ReflectionClass classes if the above doesn't work.
You cannot do $object->BOOL_CONST, since class constants have to be called statically (SomeChild::BOOLCONSTANT).
However, maybe you can try something like that: // edit: this works :)
$class = get_class($object);
$const = $class::BOOL_CONST;
Related
I want to know if there is a way to namespace values, in a similar way that we namespace functions in PHP.
You can have:
namespace pizza\land;
function turn_oven_on(){}
And you can access that function with pizza\land\hello()
I wonder if there is a way to do something similar for values.
This is not correct, I am just illustrating what I mean:
namespace pizza\land;
namespaced $ingredients = array('pepperoni', 'garlic');
Then access it with $pizza\land\ingredients.
Other parts in the same runtime can do:
namespace pasta\land;
namespaced $ingredients = array('tomato', 'mozzarella');
Then access it with $pasta\land\ingredients.
Of course that doesn't work, but it serves as an example of what I mean.
I know there is the obvious way, which would be to use the Singleton pattern where the constructor sets the value of a public property for the singleton instance (the one and only one instance of the class).
I dislike this setup, and in that case I prefer to go the killer route and just do global $pseudonamespaced_pizza_land_ingredients.
I wonder, is there anything else I can do to achieve this setup using the latest version of PHP (now 8.1)?
Why?
To have the same effect you have with global but at the same time avoid collision.
Well, let's say I am working with some procedural code and I need a value that can be accessed across multiple functions.
So I want to use that value within the realms of that bunch of functions.
I do not want to wrap all those functions into one Class and then use a property for that class, because in that case I just prefer the Singleton or the global.
Also, if not possible. Why not?
I cannot believe that this hasn't been mentioned before as something to consider for integration into PHP. So, there must be a reason for this not being possible, if it isn't. It would be a cool solution for all those codebases that are mostly procedural and use global way too often... ehem... WordPress...
I think this could be a good answer for this question as the goal is to have variable live inside of the namespace, and that OP has overcomplicated the question for no reason.
Assuming that you have a variable named $number in the global scope of a PHP script. Inside a function, we have another variable with the same name $number, and we assign and change values of it within the function. But, the global variable remains unchanged. This is variable scope. In the same way, classes can be namespaced to give it scope.
Consider having a following code:
<?php
namespace Math;
function add($a, $b) {
return $a + $b;
}
const PI = 3.14;
class Geometry {
static function getCircleArea($radius) {
return PI * $radius ** 2;
}
}
First, we declare the namespace. (So, all the classes, interfaces, constants and function below it will be items of that namespace)
Then, we declared a normal function.
Then, a class constant.
Then, a class.
Now let's access to theseMath namespace's items from another file(functions, constants, and classes).
<?php
// includes the Math.php file
// It's like you had all the code of Math.php written here
include_once 'Math.php';
echo Math\add(2,3); // 5
echo Math\PI; // 3.14
echo Math\Geometry::getCircleArea(10); // 314.15
Thanks to #shingo's comment under the question, this is how I now learned that this can be achieved.
// Namespace
namespace whatever;
// Class with static property
class StaticStuff {
static string $variable = 'Hey';
}
// Access it
echo StaticStuff::$variable; // Hey
// Edit it
StaticStuff::$variable = 'Miau!';
// Access it again
echo StaticStuff::$variable; // Miau!
Specifically:
Define a class
With a static property
Inside a namespace
I understand you cannot duplicate constant. I am just confused as to why it does not work with different objects.
In one of my project I used them to pass settings to my object.
Here is an example:
class someClass {
function __construct($config) {
define("PRIVATE_KEY", $config['private_key']);
}
}
and here is how I create the objects
$objectA = new someClass($config['A']);
$objectB = new someClass($config['B']); //ERROR!!
I get the error:
Constant PRIVATE_KEY already defined
Most people that get this error had included the same constant multiple times.
In My case they are being used in separate objects. I will add some check to make sure they are not being redefined. but I am still curious to know why this happening.
Aren't object disposed/destroyed when no longer used?
Yes, objects are destroyed at some point, but define declarations are global and persist until they are undefined. Your code is defining the same constant twice.
Private properties, static properties, or maybe class constants are more appropriate for what you're attempting to do since they are encapsulated within the object.
class someClass {
private $private_key;
// constructor
function __construct($config) {
$this->private_key = $config['private_key'];
}
}
What are you using PRIVATE_KEY for? Is it supposed to be an instance variable? If so, you shouldn't use define() because its scope is global. You could instead do $this->private_key = $config['private_key'].
I realize that this question has already been asked elsewhere for different programming languages... But this is not a 100% indicator for the same answer in the PHP domain, so I am asking this question.
Could someone please tell me what, specifically in PHP, is the difference between "final static" and "const" ?
final
The methods or classes can not be modified by a child class. This prevents class inheritance, method-overriding and/or redefinition of methods.
Only class definitions and/or methods inside a class can be defined as
final.
static
Declares class methods or properties as a static value so that you have access to them without instantiating an object. These are shared between parent and child-classes.
A class definition can not be static unlike final.
const
These create a constant value for a class. The constant values will get changed and can NOT be changed by a method in either parent or child-class.
Class constants are allocated per instance of the class.
const is a type specifier in itself. It can not be put along with public/private/static etc. final, as mentioned before can be used along with any method or class definitions and hence; applicable with all of them. static can not be applied to class definitions but can be used for class properties.
UPDATE
modifiers are allowed for class constants since PHP 7.1.0.
class Foo {
public const bar = 5;
private const baz = 6;
}
To summarise, final static can not be used to define something like:
class X {
final static x = 5;
}
which is why you have a const.
final is not for class properties, only classes and methods. It means that the method cannot be overridden, or that the class cannot be inherited from. const is the PHP equivalent to a Java final variable.
They don't have anything at all in common, and they create completely different kinds of things. const declares a constant. final static declares a method which is static (can be called without an instance of the class) and final (can't be overridden by subclasses). static alone can be used to define a class-scoped variable, which isn't constant (but variables can't be final).
This is with reference to Get a static property of an instance, I am a newbie and have the following code :
class Foo
{
public static $my_static = 1;
}
class Bar extends Foo
{
}
$foo = new Foo();
$boo = new Bar();
echo Foo::$my_static; // ok
echo Bar::$my_static; // ok
echo $foo::$my_static; // ok
echo $boo::$my_static; // ok
Static variables/properties are accessed only as ClassName::static_property as in C++, but it is not the case in PHP... but PHP books mostly mention the className::static_property pattern, not the object::static_property construct. Need more light on this..
Static properties may be accessed on various ways.
Class::$aStaticProp; //by class name
$classname::$aStaticProp; // As of PHP 5.3.0 by object instance
Static properties cannot be accessed through the object using the arrow operator ->.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).
More you can read in manual
$instance::$staticProperty is simply a convenience shorthand for Class::$staticProperty. Since you already have an instance of a class and the syntax is unambiguous, PHP saves you from writing a potentially long class name. There's no functional difference.
within the class you have to use like self::$staticPropery
if the function accessing to the variable is also static.
Can I define a class constant inside the class constructor function ?
(based on certain conditions)
That goes against the idea of class constants - they should not be dependent on a specific instance. You should use a variable instead.
However, if you insist on doing this, are very adventurous and can install PHP extensions, you can have a look at the runkit extension that allows to modify classes and their constants at runtime. See this doc: http://www.php.net/manual/en/function.runkit-constant-add.php
I don't think you can.
It wouldn't make sense, either - a class constant can be used in a static context, where there is no constructor in the first place.
You'll have to use a variable instead - that's what they're there for.
Try look here:
http://php.net/manual/en/language.oop5.constants.php
http://php.net/manual/en/language.oop5.static.php
Hope this helps.
As far as standard instance constructors go, there is no way to do this, and as others have pointed out, it wouldn't make sense. These constructors are called per created object instance, at the point they are created. There is no guarantee this constructor would get called before some code tried to access the constant. It also doesn't make sense in that the code would get called over and over again each time a new instance was constructed, whereas a const should only get set once.
It would be nice if PHP either offered some kind of static constructor that let you set the value one time for uninitialized constants, or allowed more types of expressions when defining constants. But these are not currently features of PHP. In 2015 an RFC was made that proposed adding static class constructors, but it is, at the time of me writing this answer, still in the draft status, and has not been modified since 2017.
I think the best alternative for now is to not use constants in this kind of scenario, and instead use static methods that return the value you want. This is very simple in that it only uses the PHP language features as is (not requiring any special extensions), these static methods can be called in the standard way, and you don't need to hack the autoloading process to call some kind of initializer function that sets static variables. The method might need to rely on private static variables in order to make sure the same instance is returned every time, if an object instance is being returned. You would need to write the implementation of this method to be constant like in the sense that it will always return the same thing, but takes advantage of being able to do things you can't do with a constant, like return on object instance or rely on complex expressions or function calls. Here is an example:
final class User
{
/** #var DefinitelyPositiveInt|null */ private static $usernameMaxLength;
public static function getUsernameMaxLengthConst(): DefinitelyPositiveInt
{
if ($usernameMaxLength === null) {
$usernameMaxLength = new DefinitelyPositiveInt(40);
}
return $usernameMaxLength;
}
}
$usernameInput.maxLength = User::getUsernameMaxLengthConst();
This is still not a perfect solution because it relies on the programmer to write these in a constant like way when that is desired (always returning the same value). Also, I don't like that the best place to document the fact that it is a const is in the method name, thus making it even longer to call. I also don't like that you now have to call it as a method instead of just accessing a property, which would be syntactically nicer.
This example is essentially an implementation of a singleton, but sometimes the purpose of a singleton is to be a constant rather than just a singleton. What I mean is, you might want the instance to always exist, and it might be an immutable type (none of the properties are public or mutable, only having methods that return new objects/values).
I am sorry to break it to you but it is not possible in vanilla PHP.
I am not very sure about frameworks or extensions but I am sure that it is not possible in vanilla PHP.
I recommend you to use variables instead.
You still can't, but maybe some of these (progressively weirder) ideas (just ideas, not true solutions) will work for you:
(1) You could use a private property, with a public getter method. The property cannot be modified outside the class, such as constants, but unfortunately it is accessed as a method, not as a constant, so the syntax is not the same.
class aClass{
private $const;
function __construct($const){
$this->const=$const;
}
function const(){
return $this->const;
}
}
$var1=new aClass(1);
echo $var1->const(); //Prints 1
(2) If you really want this value to be accessed as constant from outside, you can use define () inside the constructor. Unfortunately it doesn't get tied to the class or object name (as it do when you use const, using for example myClass::myConst). Furthermore, it only works if you create a single instance of the class. The second object you create is going to throw an error for redefining the constant, because is untied.
class otherClass{
function __construct($const){
define('_CONST',$const);
}
function const(){
return _CONST;
}
}
$var2=new otherClass('2');
echo $var2->const(); //Prints 2
echo _CONST; //Prints 2
#$var3=new aClass('3'); //Notice: Constant _CONST already defined
echo _CONST; //Still prints 2!
(3) Perhaps that last problem can be solved by giving variable names to the constants, related to the object to which they belong. This may be a bit weird... but maybe it works for someone.
class onemoreClass{
private $name;
function __construct($const,$name){
$this->name=$name;
$constname=$this->name."_CONST";
define($constname,$const);
}
function const(){
return constant($this->name.'_CONST');
}
}
$name='var4';
$$name=new onemoreClass(4,$name);
echo $var4->const(); //Prints 4
echo var4_CONST; //Prints 4
$name='var5';
$$name=new onemoreClass(5,$name);
echo $var5->const(); //Prints 5
echo var5_CONST; //Prints 5