6: Constructors and Destructors in OOP PHP | Object Oriented PHP Tutorial For Beginners | mmtuts


In this OOP PHP tutorial I will teach you about Constructors and Destructors which are used to run statements right before or after a class has been instantiated.

Link to this playlist:


First of all, thank you for all the support you have given me!

I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!

I am now using Patreon to share improved and updated lesson material, and for a small fee you can access all the material. I have worked hard, and done my best to help you understand what I teach.

I hope you will find it helpful 🙂

Material for this lesson:

Nguồn: https://quydinh.com

Xem thêm bài viết khác: https://quydinh.com/game


  1. What's the difference between methods and constructor?

    By both ways we can create object

    Then what's the difference?

  2. Man! You have a natural gift as a teacher. You should create a PHP course on Udemy. You will have more impact on the developer's community. Keep it up!

  3. Should i always use constructors for initialization, am asking because when i instantiate the person class the constructor expects arguments, so in the tutorial you already have Daniel his age and eye color passed as argument. Should i be initializing my instance with null values then use my regular setter method?… Say initially i want my object to have empty values…

  4. Your explanation rocks, but your multimedia sucks. Because it's highly distracting. I've found your tutorials are truly valuable, but hard to concentrate on. I'd like to thank you for your initiatives.

  5. thanks m8, deadass, my teacher sucked at teaching me this and this series just saved my ass i love you so much

  6. The destructor does not run at the same time as the constructor. Rather, it runs when the last reference to the object in question is gone, or at the end of the script.

  7. Dude your videos are AWESOME! Thanks so much for sharing this knowledge! FYI '-' is formally called a hyphen but $is_it_also->hyphen("-") also returns true. Sorry I couldn't help myself. I call it a dash more than not

  8. man PHP sure has its quirks:
    calling a method with the -> and the $ when declaring variables/properties is definitely going to take time for me to get used to. I'm so used to the . thanks to C# and JS.

  9. Nice video. One suggestion, is to do something with the destructor. Now the only thing I know is that it get's triggered already when the class is instantiated, but don't know what it's purpose or functionality would be.

  10. I just need to mention something about scopes that will be related to object destruction. All variables in the root script are in the same scope even the included scripts will have access to declared variables of their parents. Only functions will have a different scope than the scrip scope. So, with that said, objects are destroyed when they reach the end of their scope. Like instantiating an object inside a function, it will call the object destructor at the end of the function body, of course, unless it's a static variable.

  11. Could you provide an example of when you would have a PHP constructor ( _construct) using a get or post method with a HTML Form element ?

  12. I think descructor is called later:


    class Person{

    private $name;

    public function __construct($name){

    $this->name = $name;

    echo 'Constructor';


    public function __destruct(){

    echo 'Destructor';


    public function setName($name){

    $this->name = $name;


    public function getName(){

    echo $this->name;



    $person = new Person('John');



    echo 'is this end?';

    ConstructorDoeis this end?Destructor

  13. You are not talking a lot. Whatever you are doing is perfect. I like your tutorial very much. Best wishes.

  14. Hi Daniel, I just want to clarify on what will be the sense of putting public $name;

    If you will have it also in the __construct($name);

    BTW This is a way way way better series than the others 


Please enter your comment!
Please enter your name here