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

Поділитися
Вставка
  • Опубліковано 3 гру 2024

КОМЕНТАРІ • 130

  • @thegentofculture
    @thegentofculture 4 роки тому +12

    I just wish you had a video for every subject I want to study. You are probably the best youtube coding instructor I've ever seen. Seriously, I've been studying for years.

  • @alextalos6141
    @alextalos6141 2 роки тому +2

    Best teacher. Don't stop.

  • @holliegolitely6245
    @holliegolitely6245 6 місяців тому +2

    Here from the future to let you know that you did not talk too much and I understood what you said. Thank you for this series!

    • @LiteralHatReloaded
      @LiteralHatReloaded 5 місяців тому

      Yes i was just here to comment the exact same thing. its just the perfect amount needed; not redundant, but not so sparse either that you're still a bit unsure of things.

  • @lawrence744
    @lawrence744 7 місяців тому +3

    Even if you talk a lot, it makes things very clear and easy to understand. Some people might complain but I do like how you explain things. I cant wait for you to make Laravel tutorial. Please, make sure to do an example project so we can get the idea on using it for projects. Thank you

  • @dan110024
    @dan110024 3 роки тому +3

    Gotta be the best, highest quality PHP tutorials on youtube. Subbed to patreon to then forget about just to pay it back.

  • @shajibshafayet
    @shajibshafayet 5 років тому +4

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

  • @tripninaldo
    @tripninaldo 5 років тому +4

    Great! Now I can finally understand the use of "__contruct()"
    Thank you.

  • @TheOneAndOnlyAhmed
    @TheOneAndOnlyAhmed 5 років тому +14

    My first programming language is Java because of that OOP concepts are easier to me to understand thank for the tutorial ❤

  • @woodzeppelin3241
    @woodzeppelin3241 4 місяці тому

    I have been thru a few OOP PHP video tutorials, and some were good, but either explained too fast, or high level over views. From what i learned from others, viewing these videos really nailed it for me. They made sense where the others were murky. Now i am ready to convert my procedural code to OOP. Thank you and great job.

  • @Keithjo74
    @Keithjo74 5 років тому +6

    Nice and clear videos are easy to watch and learn from, Good Job.

  • @kuldimanas
    @kuldimanas 5 років тому +41

    Daniel: *making tutorials
    also Daniel: I feel like I talk a lot

  • @joem1668
    @joem1668 5 років тому +6

    mmtuts back at it again with OOP PHP

  • @muzika5001
    @muzika5001 2 роки тому

    Now watching after time again can say this is the best explanation in UA-cam regards of construct and destruct.

  • @mmelimahlobo7656
    @mmelimahlobo7656 Рік тому

    OMG your explanations are so clear,you are so helpful I am very grateful that I found your tutorials,May the Lord increase you more and more

  • @abhijitkarmakar9826
    @abhijitkarmakar9826 5 років тому +6

    An awsome way to teach PHP OOP, thanks mate, you are great (y)

  • @joejenkins9181
    @joejenkins9181 4 роки тому +1

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

  • @xavocamboful
    @xavocamboful 4 роки тому

    This course is amazing. You explain it so well. It's sensitive to those that know php, and are struggling to get their heads around OOP. Thanks so much!

  • @ntcc2821
    @ntcc2821 Рік тому

    you really teach very wellll wow, you make sure there is no confusion

  • @Stoney_Eagle
    @Stoney_Eagle 5 років тому +6

    I use construct all the times but I never use deconstruct.
    Explained as expected 😉🤘

  • @Awildjaapappeared
    @Awildjaapappeared 2 роки тому

    My man, you gained a subscriber. Just started php in my CS study and the OOP was optional for an advanced class. Wanted to be prepared so I'm following it and on the 6th video I can safely say I'm learning shitloads from these short and to the point videos. Keep it up

  • @ScottPlude
    @ScottPlude 5 років тому +4

    fantastic tutorials. I just signed up at patreon as well. Thanks for so much good info.

  • @tiagovieira6653
    @tiagovieira6653 4 роки тому +6

    If I may... You said that destruct occurs when we are done creating an object... That means that you instantiate a class then the construct runs to do the stuff we want it to do or not, and then once it's done the destruct runs... It is not like that.. Destruct run at the end of the script, and not when are are done creating the object.

  • @Lyrik-Klinge
    @Lyrik-Klinge 4 роки тому

    Perfect explaination, clear voice, great examples!

  • @knotcircle2844
    @knotcircle2844 2 роки тому

    Thanks a lot for this Dani. It's now making sense for me. I wished I saw this while on uni.

  • @LandonHughes2
    @LandonHughes2 4 роки тому +13

    The Coding Train: Never forget *this dot*
    MMTuts: Never forget *this arrow*

  • @wadiibounenni4848
    @wadiibounenni4848 9 місяців тому +1

    Clear explantation as usual :)

  • @jasonfetterly5578
    @jasonfetterly5578 4 роки тому +8

    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.

    • @mrgyani
      @mrgyani 3 роки тому

      Yeah I was wondering, if they both get run at the same time - then how are we getting the value of the properties in the next line..

    • @codewithsheikh2805
      @codewithsheikh2805 2 роки тому

      yes i will like what i had to recheck, PHP possesses a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

  • @TeejayArancina-v1o
    @TeejayArancina-v1o Місяць тому +1

    Best teacher!

  • @kyojurojigoro8677
    @kyojurojigoro8677 Рік тому

    omg i'v never thought i would enjoy oop some day thank you Brother

  • @MathijsGroothuis
    @MathijsGroothuis 4 роки тому

    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.

  • @SnGrg
    @SnGrg 2 роки тому

    Tesla, Space X and now advanced PHP tutorials. Elon is the fountain of knowledge.

  • @goranvuletic8873
    @goranvuletic8873 2 роки тому +1

    1:32 The destructor is not executed "right after creating an object" but when the object is deleted.
    4:50 You don't have to include parethesis when you have a constructor. You only need to add them if the constructor expects any parameters, which does not need to be the case, you can define the constructor without any passing parameters.

    • @Dani_Krossing
      @Dani_Krossing  2 роки тому

      Hehe I would call it “Wrong!”, but it’s nice to know that they aren’t required 🙂

    • @Dani_Krossing
      @Dani_Krossing  2 роки тому

      And yes, the destructor is called right before the object is deleted at the end of a script.
      I am not native English speaking, so when I said “it is run at the end”, I didn’t mean at the end of the class being loaded, but when the object is no longer referenced. 🙂 I can see how it comes off wrong in the way I phrased it there.

    • @goranvuletic8873
      @goranvuletic8873 2 роки тому

      @@Dani_Krossing Despite all of that, I very much enjoy your lessons and the way you have organized them.

  • @abolfazl3273
    @abolfazl3273 9 місяців тому +1

    Perfect

  • @folomba
    @folomba 2 роки тому

    Thanks! Hello from Moscow!

  • @gacembenayad4670
    @gacembenayad4670 4 роки тому +1

    Thank you boss, we are going to be rich thanks to you. G and L

    • @cutscenes3399
      @cutscenes3399 4 роки тому

      yeah bro keep dreaming untill faceing the reality

  • @LandonHughes2
    @LandonHughes2 4 роки тому +1

    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.

    • @liberationahead5020
      @liberationahead5020 4 роки тому

      I solved the problem with Karabiner Elements. Shortcut 1: Ctrl+. becomes -> and Shortcut 2: Cmd+. becomes => :-) But yes they should've just used a period!

  • @Bene31
    @Bene31 4 роки тому +1

    Summary for Java Devs:
    For constructors, use instead of "public ClassName(String s, int i)" in Java, "public function __construct(values)" in PHP.
    "$this" is equal to "this" in Java.
    You can extend classes in PHP just like in Java with "extends".
    "__destruct" is useless af (it just get's run after the "__construct", so.. use the "__construct" method).
    Everything else is pretty much the same, privat, public, protected etc.

  • @udarasnsgm
    @udarasnsgm 3 роки тому

    Thank you very much..lessons are really interesting and very easy to understand

  • @brett3554
    @brett3554 3 роки тому +1

    Great tutorial Danni

  • @danielavazquez9706
    @danielavazquez9706 5 років тому +1

    Thank you so much for your videos!!

  • @senju31
    @senju31 3 роки тому

    Much love from Jamaica!

  • @Mohammed24441
    @Mohammed24441 5 років тому +3

    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.

    • @daineldakwa1302
      @daineldakwa1302 Рік тому

      that was a good one thank you for the clarification.

    • @jayzee6432
      @jayzee6432 Рік тому

      what is a root script?

  • @johnjosephnogales9193
    @johnjosephnogales9193 Рік тому +1

    wish for you to live a long life

  • @travis-tech
    @travis-tech 3 роки тому +2

    Im learning a lot from this course. So much better then the udemy class I paid for!
    Do you cover hooks later in this course?

  • @marco114
    @marco114 4 роки тому

    thank you so much, i have been struggling with this concept.

  • @tedabrenica5277
    @tedabrenica5277 4 роки тому

    i think its better if you practices them also to end the PHP closing tags. It might help them in the future.

  • @flickerspot
    @flickerspot 3 роки тому

    Thanks. Great tutorial

  • @jasonbracero3030
    @jasonbracero3030 5 років тому +1

    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 

  • @GuruPrasad-kx4zt
    @GuruPrasad-kx4zt 4 роки тому +1

    Daniel bro god bless you

  • @ademottoman962
    @ademottoman962 5 років тому +2

    hi mmtuts can you make some more new html5 videos because i need some because you are the only one who explanes very well

  • @vanhelsingx69x
    @vanhelsingx69x Рік тому

    best tutorials

  • @mohamedannajar3140
    @mohamedannajar3140 4 роки тому

    Thank you alot for this tutorials

  • @DevlogBill
    @DevlogBill 2 роки тому

    Excellent video, my only thing would be I didn't grasp the concept of destructor, I guess I will watch the video again. Other than that, I am learning loads of information, thank you.

  • @programmingtestsandproje
    @programmingtestsandproje 3 роки тому

    thank you

  • @savilboy7269
    @savilboy7269 4 роки тому

    Bro thanks a lot super helpful, appreciate it

  • @eloquent2200
    @eloquent2200 3 роки тому

    Thank you!

  • @Voughnfox
    @Voughnfox 3 роки тому

    Super Great!!!

  • @domjag600
    @domjag600 2 роки тому

    What is the best work around to get over the limitation for overloading constructors?

  • @user-fs2zt4tk6q
    @user-fs2zt4tk6q 3 роки тому

    Legend thank you 👊🏼

  • @JAM-zr6ss
    @JAM-zr6ss Рік тому

    You look like a version of Mr. BEAST and Elon Musk combined. And thanks for this series

  • @emanuelegurini
    @emanuelegurini 5 років тому +1

    fantastic!

  • @Denniskjellin
    @Denniskjellin 2 роки тому

    Great stuff!!!!=)

  • @hacking4457
    @hacking4457 4 роки тому

    Thank u mate

  • @MediaMartPsh
    @MediaMartPsh 4 роки тому

    @mmtute what kind of laptop you're using here? Is it Lenovo Legion?

  • @sukumarsa9039
    @sukumarsa9039 5 років тому

    Just a feedback that you spoke most about what to do and how to do. But, did I miss why to do? please review.

  • @ademineshat
    @ademineshat 5 років тому

    👌 Great.

  • @RSTao77432
    @RSTao77432 5 років тому

    I know that you Cannot Nest Classes in the general sense of the Word but can you for example Create a Class With A method, Which then has Seperate Classes inside that method. That would make what i'm trying to do a hell of a lot easier... ?

  • @subgero
    @subgero 3 роки тому

    So, this is like setters and getters?

  • @arnelforbes5912
    @arnelforbes5912 5 років тому

    Its Great, you are using touch pad only

  • @MrCef1911
    @MrCef1911 5 років тому

    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 ?

  • @Redbearshit
    @Redbearshit 5 років тому

    Thank you very much! Where can I donate?

  • @mrinalsood9887
    @mrinalsood9887 2 роки тому

    Tattooed Elon Musk haha. Btw great lessons sir!!

  • @jankohrasko3
    @jankohrasko3 5 років тому +1

    Is the __construct() always public? Why not private? Thanx for the video.

    • @Dani_Krossing
      @Dani_Krossing  5 років тому +1

      Try it out :) "Learning by doing"

    • @TheOneAndOnlyAhmed
      @TheOneAndOnlyAhmed 5 років тому +2

      If it private how can you set values to the variables when you make object of the class?

    • @Dani_Krossing
      @Dani_Krossing  5 років тому

      @@TheOneAndOnlyAhmed The properties are private yes, but the constructor method is public. And because we assign the properties using the method this is possible.

  • @ThatoJChere
    @ThatoJChere 4 роки тому

    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...

    • @ApplicableProgramming
      @ApplicableProgramming 4 роки тому +1

      Both are valid ways of using it, the way he presents is called dependency injection, where his objects require some values/dependencies in order to be initiated properly. You can set them to null and then configure them when needed. So one is not better then the other, depends on how you need it

  • @hayatzee8830
    @hayatzee8830 2 роки тому

    Return 'Thank you';

  • @TheScooted
    @TheScooted 2 роки тому

    I do not fully got how exactly __destruct works. What exactly it destructs? all data for all objects that used this class? Or something else?
    Or it just runs after the object is done (so we can put some code there that we need to run at the objects work end) and it is destructed automatically?

  • @ahmaddawood9691
    @ahmaddawood9691 5 років тому

    SUPER

  • @andreabutalid1958
    @andreabutalid1958 4 роки тому

    Can I ask help what should I do for echo to show all the properties ?

    • @kvazaios5026
      @kvazaios5026 4 роки тому

      Create a method that you will return all the properties inside there and just call this method on the inddx page

    • @bhaweshthapa4672
      @bhaweshthapa4672 3 роки тому

      get_object_vars($object), an in-built PHP function

  • @abiolacomputing
    @abiolacomputing Рік тому

    You such an amazing lecturer, I had taken a course from Edwin Diaz on Udemy, He taught methods, properties, get and set, constructor etc separately because of that I am able to understand the amazing life saver lecture, newbies might be struggling as you compounded all in one go. Thank you so much for your efforts

  • @ranasheri3822
    @ranasheri3822 4 роки тому

    Is constructor important or necessary?I mean can we work without it using oop standards?

    • @ApplicableProgramming
      @ApplicableProgramming 4 роки тому +1

      Yes, it is just a principle to help you initialize object immidiatelly after it is created. You can just call your own method if you like, constructor is there just for the convenience (standard practice)

  • @pepeback
    @pepeback 5 років тому

    Better null than undefined.

  • @emmanuelsounyo2021
    @emmanuelsounyo2021 3 роки тому

    Hello please mine is not echoing as shown in the video is there a chance you linked up the includes file ; because am getting a feedback error on the line

    • @Dani_Krossing
      @Dani_Krossing  3 роки тому

      The video is a direct continuation of the last lessons, so yes the include file is linked at the top of the index page. As you can tell in the video, the top line in my index page is line 4 and it has the ?> in it, so there is the same PHP code as in the previous lessons 🙂

  • @joem1668
    @joem1668 5 років тому +1

    remember me homie

  • @workupfromthesky9870
    @workupfromthesky9870 4 роки тому

    12:03 playing Jazz piano again?

    • @thegentofculture
      @thegentofculture 4 роки тому

      yeah but I have to admit I actually find his way of using hands to talk to be engaging.

  • @prabu2778
    @prabu2778 4 роки тому

    What's the difference between methods and constructor?
    By both ways we can create object
    Then what's the difference?

    • @ApplicableProgramming
      @ApplicableProgramming 4 роки тому

      Technically, creating an object is done by compiler not methods and constructors. Constructor is not different from regular method, the only think is that if constructor is public, you are guaranteed that it will be called on object creation. So it would be the same as if you called:
      $myObj = new Obj();
      $myObj->myCustomConstructor();
      just that the php does $myObj->__construct() for you
      (same goes for destructor, but before object is going to be destroyed)

  • @raahimfareed
    @raahimfareed 4 роки тому

    Is that a Jin Tattoo on your right arm? (Tekken)

  • @lucioouy
    @lucioouy 2 роки тому

    damn i understood OOP faster than procedural programming

  • @adityatondon9493
    @adityatondon9493 5 років тому +1

    Where is destruct method?

  • @DanTrundle
    @DanTrundle 3 роки тому

    Just spent ages trying to figure out why my calls to the constructor weren't working.. always remember to double check where you saved your files, kids! You might be accessing an outdated version!

  • @spiderangelo
    @spiderangelo 3 роки тому

    you started saying about destruct, then jumped to something else. how to use destruct?

    • @tiagocomth3194
      @tiagocomth3194 3 роки тому

      To better understand the __destrust method:
      class A {
      protected $id;
      public function __construct($id)
      {
      $this->id = $id;
      echo "construct {$this->id}
      ";
      }
      public function __destruct()
      {
      echo "destruct {$this->id}
      ";
      }
      }
      $a = new A(1);
      echo "-------------
      ";
      $aa = new A(2);
      echo "=============
      ";
      The output content:
      construct 1
      -------------
      construct 2
      =============
      destruct 2
      destruct 1

  • @sarahla701
    @sarahla701 4 роки тому

    Dude your videos are AWESOME! i think i have a crush on u xD

  • @battleofviews638
    @battleofviews638 5 років тому

    I think descructor is called later:
    ----------------------------------------------------------------------
    Result:
    ConstructorDoeis this end?Destructor

  • @singlebw4065
    @singlebw4065 5 років тому

    Зашёл посмотреть и что вижу: Даниель, голубой и ему 28.

  • @adityapaluskar
    @adityapaluskar Рік тому

    _Elon Musk_ .... come from your real channel

  • @shresthakushal
    @shresthakushal Рік тому

    Is he German ?

  • @cd78
    @cd78 5 років тому +5

    OOP is overly abstracted nonsense.

    • @belinhobeli9569
      @belinhobeli9569 5 років тому

      It kind of is.

    • @Kakaohunden
      @Kakaohunden 5 років тому

      Kind of yes, but it adds a common coding standard.

    • @cd78
      @cd78 5 років тому

      @@Kakaohunden define a common coding standard??

    • @pepeback
      @pepeback 5 років тому

      You are in the wrong place

    • @andrewemerson6662
      @andrewemerson6662 5 років тому

      gl in your future endeavors

  • @NoorsDataScience
    @NoorsDataScience 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.

  • @nguyenthihang2761
    @nguyenthihang2761 5 років тому

    Your procedural course was extremely comprehensible and useful but man, this course sucks. I mean the idea is generally comprehensible but not practicle. You already have a procedural course so there's no need to use a car to explain objects. Instead just jump straight into actual coding, how and why its more efficient than procedural. Its like i have to watch a long long theory of attributes and methods of a car which has nothing to do with programming and very boring and then try to merge it into php again like how to make an sql query from a car, jesus!

    • @Dani_Krossing
      @Dani_Krossing  5 років тому

      If I go too slow for you then there are many other channels who takes less time "dumbing things down" and instead go straight to coding :)

    • @niarastikatika2904
      @niarastikatika2904 4 роки тому

      these tutorials are great. you don't know what you are saying man. so just shut your mouth

    • @amasnur79
      @amasnur79 4 роки тому +1

      For beginners like me, I appreciate very much that Daniel goes slow on the details and repeats why the coding is done certain way. It reminds me from time to time of what has been mentioned in the previous lessons, especially when I binge watch the tutorials. It also helps me to practice along with the tutorials on my own server. If you think that the lessons are slow, just fast forward, that's what the arrow buttons (or the double touch) are for.
      Daniel, keep up the good work!

  • @AxxionMarketPlace-iu6ri
    @AxxionMarketPlace-iu6ri Рік тому

    Thank you