A static property belongs to the class itself, not to any object instance. It is shared by all instances of the class.
Click to reveal answer
beginner
How do you access a static property inside a class method?
Use self::$propertyName to access a static property inside the class.
Click to reveal answer
beginner
What is a static method in PHP?
A static method belongs to the class and can be called without creating an object instance.
Click to reveal answer
beginner
How do you call a static method from outside the class?
Use ClassName::methodName() to call a static method without creating an object.
Click to reveal answer
intermediate
Can static methods access non-static properties in PHP?
No, static methods cannot access non-static properties because they do not belong to any object instance.
Click to reveal answer
How do you define a static property in a PHP class?
AUsing the <code>const</code> keyword
BUsing the <code>static</code> keyword before the property
CUsing the <code>public</code> keyword only
DUsing the <code>var</code> keyword
✗ Incorrect
Static properties are declared with the static keyword inside the class.
How do you call a static method named show in class Example?
A<code>show()</code>
B<code>Example->show()</code>
C<code>new Example()->show()</code>
D<code>Example::show()</code>
✗ Incorrect
Static methods are called using the class name and double colon: Example::show().
Which keyword is used inside a class to refer to its own static property?
Athis
Bstatic
Cself
Dparent
✗ Incorrect
Inside a class, self::$propertyName accesses static properties.
Can static properties have different values for each object instance?
ANo, static properties are shared by all instances
BYes, each object has its own copy
COnly if declared public
DOnly if declared private
✗ Incorrect
Static properties belong to the class, so all instances share the same value.
Which of the following is true about static methods?
AThey can be called without creating an object
BThey can access $this inside the method
CThey must be called on an object instance
DThey cannot access static properties
✗ Incorrect
Static methods belong to the class and can be called without creating an object.
Explain what static properties and static methods are in PHP and how they differ from regular properties and methods.
Think about how you can use a property or method without making an object.
You got /4 concepts.
Describe how to access a static property and call a static method inside and outside a PHP class.
Remember the special syntax for static members.
You got /4 concepts.
Practice
(1/5)
1. Which statement about static properties in PHP is true? class Example { public static $count = 0; }
easy
A. Static properties belong to the class itself, not to any object instance.
B. Static properties can only be accessed through object instances.
C. Static properties cannot hold values.
D. Static properties are private by default.
Solution
Step 1: Understand static property ownership
Static properties belong to the class, not to individual objects.
Step 2: Check access rules
Static properties are accessed using the class name, not object instances.
Final Answer:
Static properties belong to the class itself, not to any object instance. -> Option A
Quick Check:
Static property = class-level data [OK]
Hint: Static means class-level, not per object [OK]
Common Mistakes:
Thinking static properties belong to objects
Trying to access static properties via objects
Assuming static properties can't hold values
2. Which of the following is the correct syntax to call a static method show() of class Test?
easy
A. $obj->show();
B. $obj::show();
C. Test->show();
D. Test::show();
Solution
Step 1: Recall static method call syntax
Static methods are called using the class name and double colon (::).
Step 2: Check each option
Test::show(); uses Test::show(); which is correct syntax.
Final Answer:
Test::show(); -> Option D
Quick Check:
Static method call = ClassName::method() [OK]
Hint: Use ClassName::method() to call static methods [OK]
Common Mistakes:
Using object arrow (->) to call static methods
Using object variable with :: operator incorrectly
Using class arrow (->) which is invalid
3. What will be the output of the following PHP code?
class Counter {
public static $count = 0;
public static function increment() {
self::$count++;
}
}
Counter::increment();
Counter::increment();
echo Counter::$count;
medium
A. 1
B. 2
C. 0
D. Error
Solution
Step 1: Understand static property and method behavior
Static property $count starts at 0 and is shared by the class.
Step 2: Trace method calls
Each call to increment() increases $count by 1. Two calls increase it to 2.
Final Answer:
2 -> Option B
Quick Check:
2 increments = 2 count [OK]
Hint: Static property keeps value across calls [OK]
Common Mistakes:
Thinking static resets per call
Confusing self:: with $this->
Expecting error due to static usage
4. Identify the error in this PHP code snippet:
class Sample {
public static $value = 10;
public function show() {
echo self->$value;
}
}
$object = new Sample();
$object->show();
medium
A. Cannot use echo inside class methods.
B. Static property cannot be accessed inside non-static methods.
C. Using self::$value instead of self->$value inside the method.
D. Missing static keyword before method declaration.
Solution
Step 1: Check static property access syntax
Static properties must be accessed with self::$propertyName, not self->$propertyName.
Step 2: Verify method and property usage
The method is non-static but can access static properties using self:: syntax.
Final Answer:
Using self::$value instead of self->$value inside the method. -> Option C
Quick Check:
Static property access = self::$property [OK]
Hint: Use :: for static properties, not -> or $ [OK]
Common Mistakes:
Using $ before property name with self::
Trying to access static property with $this->
Confusing static and non-static method rules
5. You want to count how many objects of class User have been created using a static property. Which code correctly implements this?
class User {
public static $count = 0;
public function __construct() {
// What goes here?
}
}
hard
A. self::$count++;
B. $this->count++;
C. User->count++;
D. static->count++;
Solution
Step 1: Understand static property increment in constructor
Each time an object is created, the constructor runs and should increase the static count.
Step 2: Use correct syntax to increment static property
Use self::$count++; inside the constructor to increment the static property.
Final Answer:
self::$count++; -> Option A
Quick Check:
Increment static with self::$property++ [OK]
Hint: Use self::$property to access static inside class [OK]
Common Mistakes:
Using $this->count++ which accesses instance property
Using arrow (->) with class name or static keyword
Trying to increment static property without self::
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.