What is a static property in PHP?
A
A property that belongs to an object instance
B
A property that belongs to the class itself, not an object
Analysis & Theory
Static properties belong to the class itself rather than to any object instance.
Which keyword is used to declare a static property in PHP?
Analysis & Theory
The `static` keyword is used to declare static properties.
How do you access a static property?
Analysis & Theory
Static properties are accessed using the `ClassName::property` syntax.
What is the output?
```
class Counter {
public static $count = 0;
}
echo Counter::$count;
```
Analysis & Theory
The output is `0` because the static property `$count` is initialized to 0.
Can static properties be accessed without creating an object?
D
Only inside constructor
Analysis & Theory
Yes, static properties can be accessed directly using the class name without creating an object.
Which keyword is used inside a class to refer to its own static property?
Analysis & Theory
The `self` keyword is used to refer to static properties within the same class.
Can static properties be private or protected?
B
Yes, they can be public, private, or protected
D
Only public and protected
Analysis & Theory
Static properties can have any visibility: public, private, or protected.
What happens if two objects change a static property?
A
Each object has its own copy of the static property
B
The static property is shared among all instances
D
It creates multiple copies
Analysis & Theory
Static properties are shared among all instances of a class.
Can static properties be accessed from static methods?
Analysis & Theory
Yes, static properties can be accessed from static methods using `self::$property` or `static::$property`.
Why are static properties useful in PHP?
A
To store values shared across all instances of a class
Analysis & Theory
Static properties are useful for maintaining data shared across all instances, such as counters or configuration values.