0
0
PHPprogramming~10 mins

__construct and __destruct in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - __construct and __destruct
Create Object
Call __construct
Use Object
Object No Longer Needed
Call __destruct
Object Destroyed
When an object is created, __construct runs automatically to set it up. When the object is no longer needed, __destruct runs to clean up.
Execution Sample
PHP
<?php
class Demo {
  function __construct() {
    echo "Start\n";
  }
  function __destruct() {
    echo "End\n";
  }
}
$obj = new Demo();
?>
This code creates an object that prints 'Start' when made and 'End' when destroyed.
Execution Table
StepActionOutputNotes
1Create object with new Demo()Start__construct runs and prints 'Start'
2Object exists and can be usedNo output, object ready
3Script ends or object unsetEnd__destruct runs and prints 'End'
💡 Object destroyed at script end, __destruct called
Variable Tracker
VariableStartAfter CreationAfter Script Ends
$objundefinedobject Demo instancedestroyed (no longer exists)
Key Moments - 3 Insights
Why does __construct run automatically without calling it?
__construct is a special method PHP calls automatically when you create a new object, as shown in step 1 of the execution table.
When exactly does __destruct run?
__destruct runs when the object is destroyed, usually at the end of the script or when the object is unset, as shown in step 3.
Can __destruct run multiple times for the same object?
No, __destruct runs only once when the object is destroyed, which happens once per object lifecycle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed immediately after creating the object?
ANothing
B"Start"
C"End"
DAn error message
💡 Hint
Check step 1 in the execution table where __construct runs.
At which step does __destruct run according to the execution table?
AStep 3
BStep 2
CStep 1
DNever
💡 Hint
Look for when 'End' is printed in the output column.
If we unset the object before script ends, when will __destruct run?
AAt script end only
BNever
CImmediately when unset is called
DBefore __construct
💡 Hint
Recall __destruct runs when object is destroyed, which can happen on unset or script end.
Concept Snapshot
__construct and __destruct in PHP:
- __construct runs automatically when an object is created.
- Use __construct to initialize object properties.
- __destruct runs automatically when object is destroyed.
- Use __destruct to clean up resources.
- Both methods do not need to be called manually.
Full Transcript
In PHP, __construct is a special method that runs automatically when you create a new object. It helps set up the object, like turning on a machine when you start it. __destruct runs automatically when the object is no longer needed, like turning off the machine to save power. In the example, when we create the object, __construct prints 'Start'. When the script ends or the object is destroyed, __destruct prints 'End'. This automatic calling helps manage setup and cleanup without extra code.