Bird
Raised Fist0
PHPprogramming~10 mins

Methods and $this keyword in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the method inside the class using $this.

PHP
<?php
class Car {
    public function start() {
        echo "Car started";
    }
    public function run() {
        $this->[1]();
    }
}
$car = new Car();
$car->run();
?>
Drag options to blanks, or click blank then click option'
Astop
Brun
Cstart
Ddrive
Attempts:
3 left
💡 Hint
Common Mistakes
Using the method name without $this->
Calling a method that does not exist
2fill in blank
medium

Complete the code to access the property inside the method using $this.

PHP
<?php
class Person {
    public $name = "Alice";
    public function greet() {
        echo "Hello, " . $this->[1];
    }
}
$person = new Person();
$person->greet();
?>
Drag options to blanks, or click blank then click option'
Aname
Bhello
Cgreet
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access property without $this->
Using a wrong property name
3fill in blank
hard

Fix the error in the method to correctly return the object's property using $this.

PHP
<?php
class Book {
    public $title = "PHP Basics";
    public function getTitle() {
        return [1]->title;
    }
}
$book = new Book();
echo $book->getTitle();
?>
Drag options to blanks, or click blank then click option'
Athis
B$this
C$self
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' without $ sign
Using 'self' which refers to the class, not the object instance
4fill in blank
hard

Fill both blanks to create a method that sets the object's property using $this.

PHP
<?php
class Laptop {
    public $brand;
    public function setBrand([1]) {
        $this->[2] = $brand;
    }
}
$laptop = new Laptop();
$laptop->setBrand("Dell");
echo $laptop->brand;
?>
Drag options to blanks, or click blank then click option'
Astring $brand
Bbrand
Cmodel
Dprice
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a parameter in the method
Assigning to a wrong property name
5fill in blank
hard

Fill all three blanks to create a method that updates and returns the object's property using $this.

PHP
<?php
class Phone {
    public $color = "Black";
    public function changeColor([1]) {
        $this->[2] = $newColor;
        return $this->[3];
    }
}
$phone = new Phone();
echo $phone->changeColor("Red");
?>
Drag options to blanks, or click blank then click option'
Astring $newColor
Bcolor
DnewColor
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name
Returning a variable instead of the property

Practice

(1/5)
1. What does the $this keyword represent inside a PHP class method?
easy
A. A global variable
B. A static variable
C. The current object instance
D. The class name

Solution

  1. Step 1: Understand the role of $this in PHP classes

    $this is used inside class methods to refer to the current object instance that called the method.
  2. Step 2: Differentiate from other options

    It is not a static or global variable, nor does it represent the class name itself.
  3. Final Answer:

    The current object instance -> Option C
  4. Quick Check:

    $this = current object instance [OK]
Hint: Remember: $this always means 'this object' [OK]
Common Mistakes:
  • Confusing $this with static variables
  • Thinking $this is the class name
  • Assuming $this is a global variable
2. Which of the following is the correct way to call a method named greet inside a PHP class using $this?
easy
A. this->greet();
B. self->greet();
C. $this.greet();
D. $this->greet();

Solution

  1. Step 1: Recall PHP method call syntax inside classes

    Methods are called on objects using the arrow operator -> with $this representing the current object.
  2. Step 2: Check each option

    $this->greet(); is the correct syntax. The other options use invalid syntax.
  3. Final Answer:

    $this->greet(); -> Option D
  4. Quick Check:

    Method call syntax = $this->method(); [OK]
Hint: Use $this->methodName(); to call methods inside class [OK]
Common Mistakes:
  • Using dot (.) instead of arrow (->) operator
  • Omitting the dollar sign in $this
  • Using self without scope resolution operator
3. What will be the output of the following PHP code?
class Person {
  public $name = 'Alice';
  public function sayName() {
    return 'Name: ' . $this->name;
  }
}
$p = new Person();
echo $p->sayName();
medium
A. Name: Alice
B. Name: $name
C. Name:
D. Error: Undefined property

Solution

  1. Step 1: Understand property access with $this

    The method sayName() returns the string 'Name: ' concatenated with the object's name property accessed via $this->name.
  2. Step 2: Check the property value and output

    The property name is set to 'Alice', so the output will be 'Name: Alice'.
  3. Final Answer:

    Name: Alice -> Option A
  4. Quick Check:

    Property accessed via $this = 'Alice' [OK]
Hint: Remember: $this->property accesses current object's data [OK]
Common Mistakes:
  • Forgetting to use $this to access properties
  • Expecting literal string output without variable substitution
  • Confusing property name with string '$name'
4. Identify the error in this PHP class method:
class Car {
  public $model = 'Tesla';
  public function getModel() {
    return $model;
  }
}
medium
A. Missing $this when accessing $model
B. Method name should be GetModel with capital G
C. Property $model must be private
D. Return statement should be return $this->getModel;

Solution

  1. Step 1: Check property access inside method

    Inside class methods, properties must be accessed with $this->propertyName. Here, $model is used without $this->.
  2. Step 2: Validate other options

    Method names are case-insensitive in PHP, property visibility is not an error here, and the suggested return $this->getModel; is incorrect.
  3. Final Answer:

    Missing $this when accessing $model -> Option A
  4. Quick Check:

    Use $this->property to access properties [OK]
Hint: Always use $this-> to access object properties inside methods [OK]
Common Mistakes:
  • Accessing properties without $this->
  • Confusing method name case sensitivity
  • Misunderstanding property visibility rules
5. Given the class below, what will be the output?
class Counter {
  private $count = 0;
  public function increment() {
    $this->count++;
  }
  public function getCount() {
    return $this->count;
  }
}
$c = new Counter();
$c->increment();
$c->increment();
echo $c->getCount();
hard
A. Error: Cannot access private property
B. 2
C. 1
D. 0

Solution

  1. Step 1: Understand property increment inside the class

    The increment() method increases the private $count property by 1 each time it is called.
  2. Step 2: Trace method calls and final output

    Calling increment() twice increases $count from 0 to 2. Then getCount() returns 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Two increments = count 2 [OK]
Hint: Private properties accessed via $this inside class work fine [OK]
Common Mistakes:
  • Assuming private properties can't be accessed inside class methods
  • Forgetting that increment operator changes value
  • Expecting output before increments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard