Apart from the apparent difference of
- having to declare the value at the time of a definition for a
const
VS readonly
values can be computed dynamically but need to be assigned before the constructor exits.. after that it is frozen.
const
's are implicitly static
. You use a ClassName.ConstantName
notation to access them.
There is a subtle difference. Consider a class defined in AssemblyA
.
public class Const_V_Readonly
{
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
public Const_V_Readonly()
{
I_RO_VALUE = 3;
}
}
AssemblyB
references AssemblyA
and uses these values in code. When this is compiled:
- in the case of the
const
value, it is like a find-replace. The value 2 is 'baked into' the AssemblyB
's IL. This means that if tomorrow I update I_CONST_VALUE
to 20, AssemblyB
would still have 2 till I recompile it.
- in the case of the
readonly
value, it is like a ref
to a memory location. The value is not baked into AssemblyB
's IL. This means that if the memory location is updated, AssemblyB
gets the new value without recompilation. So if I_RO_VALUE
is updated to 30, you only need to build AssemblyA
and all clients do not need to be recompiled.
So if you are confident that the value of the constant won't change, use a const
.
public const int CM_IN_A_METER = 100;
But if you have a constant that may change (e.g. w.r.t. precision).. or when in doubt, use a readonly
.
public readonly float PI = 3.14;
Update: Aku needs to get a mention because he pointed this out first. Also I need to plug where I learned this: Effective C# - Bill Wagner
Short Answer
Use $this
to refer to the current
object. Use self
to refer to the
current class. In other words, use
$this->member
for non-static members,
use self::$member
for static members.
Full Answer
Here is an example of correct usage of $this
and self
for non-static and static member variables:
<?php
class X {
private $non_static_member = 1;
private static $static_member = 2;
function __construct() {
echo $this->non_static_member . ' '
. self::$static_member;
}
}
new X();
?>
Here is an example of incorrect usage of $this
and self
for non-static and static member variables:
<?php
class X {
private $non_static_member = 1;
private static $static_member = 2;
function __construct() {
echo self::$non_static_member . ' '
. $this->static_member;
}
}
new X();
?>
Here is an example of polymorphism with $this
for member functions:
<?php
class X {
function foo() {
echo 'X::foo()';
}
function bar() {
$this->foo();
}
}
class Y extends X {
function foo() {
echo 'Y::foo()';
}
}
$x = new Y();
$x->bar();
?>
Here is an example of suppressing polymorphic behaviour by using self
for member functions:
<?php
class X {
function foo() {
echo 'X::foo()';
}
function bar() {
self::foo();
}
}
class Y extends X {
function foo() {
echo 'Y::foo()';
}
}
$x = new Y();
$x->bar();
?>
The idea is that $this->foo()
calls the foo()
member function of whatever is the exact type of the current object. If the object is of type X
, it thus calls X::foo()
. If the object is of type Y
, it calls Y::foo()
. But with self::foo(), X::foo()
is always called.
From http://www.phpbuilder.com/board/showthread.php?t=10354489:
By http://board.phpbuilder.com/member.php?145249-laserlight
Best Answer
As of PHP 5.3 there are two ways to define constants: Either using the
const
keyword or using thedefine()
function:The fundamental difference between those two ways is that
const
defines constants at compile time, whereasdefine
defines them at run time. This causes most ofconst
's disadvantages. Some disadvantages ofconst
are:const
cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:Why would you want to do that anyway? One common application is to check whether the constant is already defined:
const
accepts a static scalar (number, string or other constant liketrue
,false
,null
,__FILE__
), whereasdefine()
takes any expression. Since PHP 5.6 constant expressions are allowed inconst
as well:const
takes a plain constant name, whereasdefine()
accepts any expression as name. This allows to do things like this:const
s are always case sensitive, whereasdefine()
allows you to define case insensitive constants by passingtrue
as the third argument (Note: defining case-insensitive constants is deprecated as of PHP 7.3.0 and removed since PHP 8.0.0):So, that was the bad side of things. Now let's look at the reason why I personally always use
const
unless one of the above situations occurs:const
simply reads nicer. It's a language construct instead of a function and also is consistent with how you define constants in classes.const
, being a language construct, can be statically analysed by automated tooling.const
defines a constant in the current namespace, whiledefine()
has to be passed the full namespace name:Since PHP 5.6
const
constants can also be arrays, whiledefine()
does not support arrays yet. However, arrays will be supported for both cases in PHP 7.Finally, note that
const
can also be used within a class or interface to define a class constant or interface constant.define
cannot be used for this purpose:Summary
Unless you need any type of conditional or expressional definition, use
const
s instead ofdefine()
s - simply for the sake of readability!