问题
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.
回答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.
回答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
}
}
回答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:
- PHP - Inheritance
回答4:
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)
回答5:
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...'
回答6:
The reason you'd want to use a static member variable/function is because you can extract information about that class without needing to create an instance of it (which costs you less CPU overhead).
回答7:
Constant variable are separate for each and every object. But static variable are common for all object of that class. Each object share a single static variable. Static variable created at Class Level. Constant variable created at instance level.
来源:https://stackoverflow.com/questions/4163975/php-oop-constant-vs-static-variables