Bird
Raised Fist0
PHPprogramming~20 mins

Destructor method in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Destructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP destructor example?

Consider the following PHP code. What will it output when run?

PHP
<?php
class Test {
    public function __construct() {
        echo "Constructed\n";
    }
    public function __destruct() {
        echo "Destructed\n";
    }
}
$obj = new Test();
echo "End of script\n";
?>
A
Constructed
Destructed
End of script
B
Constructed
End of script
Destructed
C
Destructed
Constructed
End of script
D
End of script
Constructed
Destructed
Attempts:
2 left
💡 Hint

Remember that the destructor runs when the object is destroyed, usually at the end of the script.

❓ Predict Output
intermediate
2:00remaining
What happens when an object is unset explicitly?

What will this PHP code output?

PHP
<?php
class Sample {
    public function __destruct() {
        echo "Bye!\n";
    }
}
$obj = new Sample();
unset($obj);
echo "After unset\n";
?>
A
Bye!
B
After unset
Bye!
C
Bye!
After unset
D
After unset
Attempts:
2 left
💡 Hint

Think about when the destructor is called if you unset the object manually.

🔧 Debug
advanced
2:00remaining
Why does this destructor not run as expected?

Look at this PHP code. The destructor message is never printed. Why?

PHP
<?php
class Demo {
    public function __destruct() {
        echo "Destroying\n";
    }
}
function create() {
    $obj = new Demo();
}
create();
echo "Done\n";
?>
AThe destructor is not called because PHP requires explicit unset to run destructor.
BThe destructor is never called because the object is inside a function and goes out of scope silently.
CThe destructor is called but output is buffered and not shown.
DThe object is destroyed immediately after create() ends, so destructor runs before echo "Done".
Attempts:
2 left
💡 Hint

Think about when local variables inside functions are destroyed.

📝 Syntax
advanced
2:00remaining
Which option shows the correct destructor syntax in PHP?

Choose the correct way to declare a destructor method in a PHP class.

Apublic function __destruct() {}
Bpublic function destruct() {}
Cfunction __destruct {}
Dpublic function ~destruct() {}
Attempts:
2 left
💡 Hint

Destructor method name must be exactly __destruct with parentheses.

🚀 Application
expert
3:00remaining
What is the output when multiple objects with destructors are created and unset?

Consider this PHP code. What will be the exact output?

PHP
<?php
class A {
    public function __destruct() {
        echo "A destroyed\n";
    }
}
class B {
    public function __destruct() {
        echo "B destroyed\n";
    }
}
$a = new A();
$b = new B();
unset($a);
echo "Middle\n";
?>
A
A destroyed
Middle
B destroyed
B
Middle
A destroyed
B destroyed
C
A destroyed
B destroyed
Middle
D
B destroyed
A destroyed
Middle
Attempts:
2 left
💡 Hint

Think about when each object is destroyed and when the script ends.

Practice

(1/5)
1. What is the purpose of the __destruct method in a PHP class?
easy
A. To clean up resources when an object is destroyed
B. To initialize object properties when created
C. To define a class constant
D. To handle user input validation

Solution

  1. Step 1: Understand the role of __destruct

    The __destruct method is called automatically when an object is destroyed or goes out of scope.
  2. Step 2: Identify its purpose

    It is used to clean up resources like closing files or database connections safely.
  3. Final Answer:

    To clean up resources when an object is destroyed -> Option A
  4. Quick Check:

    Destructor method = cleanup on object destruction [OK]
Hint: Destructor cleans up when object is removed from memory [OK]
Common Mistakes:
  • Confusing __destruct with __construct
  • Thinking it initializes objects
  • Using it to handle input validation
2. Which of the following is the correct syntax to declare a destructor method in PHP?
easy
A. function __destruct() {}
B. function destruct() {}
C. function _destruct() {}
D. function __destroy() {}

Solution

  1. Step 1: Recall PHP destructor naming

    PHP destructor methods must be named exactly __destruct with two underscores.
  2. Step 2: Check syntax correctness

    The correct syntax is function __destruct() {}. Other options have incorrect names.
  3. Final Answer:

    function __destruct() {} -> Option A
  4. Quick Check:

    Destructor syntax = __destruct() [OK]
Hint: Destructor always starts with double underscores __destruct [OK]
Common Mistakes:
  • Using single underscore or wrong method name
  • Omitting parentheses in method declaration
  • Confusing with __construct method
3. What will be the output of the following PHP code?
class Test {
  function __construct() {
    echo "Constructed\n";
  }
  function __destruct() {
    echo "Destructed\n";
  }
}
$obj = new Test();
echo "Hello\n";
unset($obj);
medium
A. Constructed\nDestructed\nHello\n
B. Hello\nConstructed\nDestructed\n
C. Destructed\nConstructed\nHello\n
D. Constructed\nHello\nDestructed\n

Solution

  1. Step 1: Trace object creation and constructor call

    When $obj = new Test(); runs, __construct() prints "Constructed\n".
  2. Step 2: Print "Hello" and then unset object

    Next, "Hello\n" is printed. Then unset($obj); destroys the object, triggering __destruct() which prints "Destructed\n".
  3. Final Answer:

    Constructed\nHello\nDestructed\n -> Option D
  4. Quick Check:

    Constructor then echo then destructor [OK]
Hint: Destructor runs when object is unset or script ends [OK]
Common Mistakes:
  • Assuming destructor runs before echo
  • Confusing order of constructor and destructor
  • Missing that unset triggers destructor immediately
4. Identify the error in this PHP class related to the destructor method:
class Sample {
  function __destruct {
    echo "Cleaning up";
  }
}
medium
A. Destructor must be static
B. Destructor name should be __destroy
C. Missing parentheses in destructor declaration
D. Destructor cannot echo text

Solution

  1. Step 1: Check destructor method syntax

    The destructor method must include parentheses even if it has no parameters: function __destruct() {}.
  2. Step 2: Identify the missing parentheses error

    The code misses parentheses after __destruct, causing a syntax error.
  3. Final Answer:

    Missing parentheses in destructor declaration -> Option C
  4. Quick Check:

    Destructor needs () after name [OK]
Hint: Always add () after __destruct method name [OK]
Common Mistakes:
  • Omitting parentheses in method declaration
  • Using wrong destructor name
  • Thinking destructor must be static
5. You want to ensure a file is always closed when your PHP object is destroyed. Which code snippet correctly uses a destructor to do this?
class FileHandler {
  private $file;
  function __construct($filename) {
    $this->file = fopen($filename, 'w');
  }
  // What goes here?
}
hard
A. function __destruct() { open($this->file); }
B. function __destruct() { fclose($this->file); }
C. function __destruct() { fclose($filename); }
D. function __destruct() { $this->file = null; }

Solution

  1. Step 1: Understand resource cleanup in destructor

    The destructor should close the file resource held in $this->file using fclose().
  2. Step 2: Check each option for correctness

    function __destruct() { fclose($this->file); } correctly calls fclose($this->file);. function __destruct() { open($this->file); } tries to open instead of close. function __destruct() { fclose($filename); } uses undefined $filename. function __destruct() { $this->file = null; } just sets property to null without closing.
  3. Final Answer:

    function __destruct() { fclose($this->file); } -> Option B
  4. Quick Check:

    Destructor closes file resource properly [OK]
Hint: Destructor should close resources like files with fclose() [OK]
Common Mistakes:
  • Calling wrong function like open() instead of fclose()
  • Using undefined variables inside destructor
  • Not actually closing the resource, just nullifying
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