0
0
PHPprogramming~5 mins

Static properties and methods in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a static property in PHP?
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
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>
Which keyword is used inside a class to refer to its own static property?
Athis
Bstatic
Cself
Dparent
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
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
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.