PHP OOP - constant vs static variables?
Solution 1
Static is for:
class properties or methods as static makes them accessible without needing an instantiation of the class
So, the value returned by a static member may differ. For example, you can call a static method with different result depending of what parameters you pass to it.
Constants value:
must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
So, it always return the same result when you call it
About create an object and extending a class, when you "create an object" you make an instance of a class. When you extend a class, you create an other class who:
inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.
I hope it help you.
Solution 2
Comparison
Static
-
Can has a access modifier.
class A{ public static $public_static = "can access from anywhere"; protected static $protected_static = "can access from inheriting classes"; private static $private_static = "can access only inside the class"; }
-
Depending the visibility you can access static variables.
//inside the class self::$variable_name; static::$variable_name; //outside the class class_name::$variable_name;
-
Can change the value after declaration.
self::$variable_name = "New Value"; static::$variable_name = "New Value";
-
No need to initialize when declare.
public static $variable_name;
-
Normal variable declaration rules applied(ex: begins with $)
-
Can create inside a function.
class A{ function my_function(){ static $val = 12; echo ++$val; //13 } }
Constant
-
Always public cannot put access modifiers.
class A{ const my_constant = "constant value"; public const wrong_constant="wrong" // produce a parse error }
-
Anywhere you can access constant.
//inside the class self::variable_name; static::variable_name; //outside the class class_name::variable_name;
-
Cannot change the value after declaration.
self::variable_name = "cannot change"; //produce a parse error
-
Must initialize when declare.
class A{ const my_constant = "constant value";// Fine const wrong_constant;// produce a parse error }
-
Must not use $ in the beginning of the variable(Other variable rules applied).
class A{ const my_constant = "constant value";// Fine const $wrong_constant="wrong";// produce a parse error }
-
Cannot declare inside a function.
When Extending
class A{
public static $public_static = "can access from anywhere";
protected static $protected_static = "can access from inheriting classes";
private static $private_static = "can access only inside the class";
const my_constant = "Constant value";
}
class B extends A{
function output(){
// you can use self or static
echo self::$public_static; //can access from anywhere;
echo self::$protected_static; //can access from inheriting classes;
self::$protected_static = "Changed value from Class B";
echo self::$protected_static; //"Changed value from Class B";
echo self::$private_static; //Produce Fatal Error
echo self::my_constant;//Constant value
}
}
Solution 3
A constant is constant and can NOT change its value once assigned. A static variable, on the other hand, can have varying values. For example, you can create a static variable inside a function to know how many time the function was called. The value will change each time function is called eg if you do $i++
where $i
is static
variable.
As for extending a class and creating its object
, this is known as inheritance, check out this post to know more about it:
Solution 4
One important difference is in memory allocation.
When an instance of a class (object) is created, memory is allocated for the newly created object. The name static is after the nature of the memory allocation. That is, memory for static objects are allocated only once, statically. When an object changes it's static property, it reflects on all objects of the same class.
<?php
class Test {
static $test = "world!";
static function hello() {
echo "\nhello " . self::$test . "\n";
}
}
$t = new Test();
$x = new Test();
$t->hello(); // prints 'hello world!'
$t::$test = "Hmmm...";
$t->hello(); // prints 'hello Hmmm...'
$x->hello(); // prints 'hello Hmmm...'
Solution 5
Constant variable is a variable which can be accessed by class name and can NOT be changed during script execution. Class static variable also can be accessed by class name but can be changed during program execution.
Second question - these are COMPLETELY other things. Read more about object oriented programming (not only in PHP)
Comments
-
pMan almost 2 years
In PHP, What is is the difference between:
- Constants and static variables?
- Extending a class and creating its object?
I know how they can be used, but I can't clearly distinguish between them.
-
DanMan over 8 years6. is comparing apples with oranges since local static variables are not the same as static (or constant) class properties. They only exist inside that function/method.
-
JCarlosR over 6 yearscan't use constants from objects? (instead from the class)
-
manniL almost 4 yearsYou can't access constants outside classes though
-
Shadi almost 4 yearsPlease note that starting from PHP7.1 access modifiers are allowed for constants, so one can have:
private const my_constant = "constant value";
.