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

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024

ความคิดเห็น • 129

  • @thegentofculture
    @thegentofculture 3 ปีที่แล้ว +11

    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.

  • @holliegolitely6245
    @holliegolitely6245 3 หลายเดือนก่อน +1

    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 3 หลายเดือนก่อน

      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.

  • @dan110024
    @dan110024 3 ปีที่แล้ว +2

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

  • @kuldimanas
    @kuldimanas 5 ปีที่แล้ว +41

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

  • @shajibshafayet
    @shajibshafayet 4 ปีที่แล้ว +3

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

  • @lawrence744
    @lawrence744 5 หลายเดือนก่อน +2

    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

  • @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 หลายเดือนก่อน

    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.

  • @tripninaldo
    @tripninaldo 5 ปีที่แล้ว +4

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

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

  • @LandonHughes2
    @LandonHughes2 4 ปีที่แล้ว +13

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

  • @abhijitkarmakar9826
    @abhijitkarmakar9826 5 ปีที่แล้ว +6

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

  • @joem1668
    @joem1668 5 ปีที่แล้ว +6

    mmtuts back at it again with OOP PHP

  • @ScottPlude
    @ScottPlude 4 ปีที่แล้ว +4

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

  • @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 ปีที่แล้ว

      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.

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

  • @alextalos6141
    @alextalos6141 2 ปีที่แล้ว +1

    Best teacher. Don't stop.

  • @Stoney_Eagle
    @Stoney_Eagle 5 ปีที่แล้ว +6

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

  • @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 3 ปีที่แล้ว

    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!

  • @Awildjaapappeared
    @Awildjaapappeared ปีที่แล้ว

    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

  • @SnGrg
    @SnGrg 2 ปีที่แล้ว

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

  • @muzika5001
    @muzika5001 2 ปีที่แล้ว

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

  • @wadiibounenni4848
    @wadiibounenni4848 6 หลายเดือนก่อน +1

    Clear explantation as usual :)

  • @ntcc2821
    @ntcc2821 ปีที่แล้ว

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

  • @folomba
    @folomba 2 ปีที่แล้ว

    Thanks! Hello from Moscow!

  • @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?

  • @kyojurojigoro8677
    @kyojurojigoro8677 ปีที่แล้ว

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

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

  • @senju31
    @senju31 3 ปีที่แล้ว

    Much love from Jamaica!

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

  • @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 3 ปีที่แล้ว

      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!

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

  • @brett3554
    @brett3554 3 ปีที่แล้ว +1

    Great tutorial Danni

  • @domjag600
    @domjag600 2 ปีที่แล้ว

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

  • @Mohammed24441
    @Mohammed24441 4 ปีที่แล้ว +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?

  • @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 

  • @johnjosephnogales9193
    @johnjosephnogales9193 10 หลายเดือนก่อน +1

    wish for you to live a long life

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

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

  • @tedabrenica5277
    @tedabrenica5277 3 ปีที่แล้ว

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

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

  • @danielavazquez9706
    @danielavazquez9706 5 ปีที่แล้ว +1

    Thank you so much for your videos!!

  • @udarasnsgm
    @udarasnsgm 3 ปีที่แล้ว

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

  • @JAM-zr6ss
    @JAM-zr6ss ปีที่แล้ว

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

  • @flickerspot
    @flickerspot 3 ปีที่แล้ว

    Thanks. Great tutorial

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

  • @marco114
    @marco114 4 ปีที่แล้ว

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

  • @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?

  • @programmingtestsandproje
    @programmingtestsandproje 3 ปีที่แล้ว

    thank you

  • @Voughnfox
    @Voughnfox 3 ปีที่แล้ว

    Super Great!!!

  • @vanhelsingx69x
    @vanhelsingx69x ปีที่แล้ว

    best tutorials

  • @taospencer1403
    @taospencer1403 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?

  • @abolfazl3273
    @abolfazl3273 7 หลายเดือนก่อน +1

    Perfect

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

  • @eloquent2200
    @eloquent2200 2 ปีที่แล้ว

    Thank you!

  • @MrCef1911
    @MrCef1911 4 ปีที่แล้ว

    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 ?

  • @Denniskjellin
    @Denniskjellin 2 ปีที่แล้ว

    Great stuff!!!!=)

  • @emanuelegurini
    @emanuelegurini 5 ปีที่แล้ว +1

    fantastic!

  • @GuruPrasad-kx4zt
    @GuruPrasad-kx4zt 4 ปีที่แล้ว +1

    Daniel bro god bless you

  • @savilboy7269
    @savilboy7269 4 ปีที่แล้ว

    Bro thanks a lot super helpful, appreciate it

  • @mohamedannajar3140
    @mohamedannajar3140 4 ปีที่แล้ว

    Thank you alot for this tutorials

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

  • @Redbearshit
    @Redbearshit 5 ปีที่แล้ว

    Thank you very much! Where can I donate?

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

  • @user-fs2zt4tk6q
    @user-fs2zt4tk6q 3 ปีที่แล้ว

    Legend thank you 👊🏼

  • @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 🙂

  • @mrinalsood9887
    @mrinalsood9887 2 ปีที่แล้ว

    Tattooed Elon Musk haha. Btw great lessons sir!!

  • @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)

  • @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)

  • @ademineshat
    @ademineshat 5 ปีที่แล้ว

    👌 Great.

  • @hacking4457
    @hacking4457 4 ปีที่แล้ว

    Thank u mate

  • @arnelforbes5912
    @arnelforbes5912 4 ปีที่แล้ว

    Its Great, you are using touch pad only

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

  • @raahimfareed
    @raahimfareed 4 ปีที่แล้ว

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

  • @adityatondon9493
    @adityatondon9493 5 ปีที่แล้ว +1

    Where is destruct method?

  • @lucioouy
    @lucioouy ปีที่แล้ว

    damn i understood OOP faster than procedural programming

  • @andreabutalid1958
    @andreabutalid1958 4 ปีที่แล้ว

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

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

  • @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!

  • @hayatzee8830
    @hayatzee8830 2 ปีที่แล้ว

    Return 'Thank you';

  • @pepeback
    @pepeback 5 ปีที่แล้ว

    Better null than undefined.

  • @ahmaddawood9691
    @ahmaddawood9691 5 ปีที่แล้ว

    SUPER

  • @joem1668
    @joem1668 5 ปีที่แล้ว +1

    remember me homie

  • @workupfromthesky9870
    @workupfromthesky9870 4 ปีที่แล้ว

    12:03 playing Jazz piano again?

    • @thegentofculture
      @thegentofculture 3 ปีที่แล้ว

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

  • @battleofviews638
    @battleofviews638 4 ปีที่แล้ว

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

  • @sarahla701
    @sarahla701 4 ปีที่แล้ว

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

  • @adityapaluskar
    @adityapaluskar ปีที่แล้ว

    _Elon Musk_ .... come from your real channel

  • @shresthakushal
    @shresthakushal 9 หลายเดือนก่อน

    Is he German ?

  • @singlebw4065
    @singlebw4065 5 ปีที่แล้ว

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

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