0
0
PHPprogramming~5 mins

Methods and $this keyword in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a method in PHP classes?
A method is a function defined inside a class that describes behaviors or actions objects of that class can perform.
Click to reveal answer
beginner
What does the <code>$this</code> keyword represent inside a PHP class method?
$this refers to the current object instance that is calling the method. It allows access to the object's properties and other methods.
Click to reveal answer
intermediate
How do you call a method from inside another method in the same PHP class?
Use $this->methodName() to call another method of the same object.
Click to reveal answer
intermediate
Why can't you use $this in static methods?
Static methods belong to the class itself, not to any object instance, so <code>$this</code> (which refers to an object) is not available.
Click to reveal answer
beginner
Example: What will $this->name access inside a method?
It accesses the name property of the current object instance.
Click to reveal answer
Inside a PHP class method, what does $this refer to?
AThe current object instance
BThe class itself
CA global variable
DA static method
How do you call a method named greet from another method in the same PHP class?
Agreet()
Bself::greet()
C$this->greet()
DClassName::greet()
Can you use $this inside a static method?
AOnly if the method is private
BYes, always
COnly if the method is public
DNo, because static methods do not belong to an object instance
What will $this->property access inside a method?
AA global variable
BThe property of the current object
CA static property
DA local variable
Which keyword is used to refer to the current object inside a PHP class?
A$this
Bself
Cparent
Dstatic
Explain what $this means in PHP and how it is used inside class methods.
Think about how an object refers to itself.
You got /4 concepts.
    Describe how to call one method from another method inside the same PHP class.
    Remember how you access properties with $this->property.
    You got /4 concepts.