PHP Autoload and Namespaces [Extended Guide] | [2020]

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • Complete Tutorial on PHP autoloading with the basics of namespace and composer and how they help in Autoloading PHP classes.
    To skip the Introduction Jump to Autoloading: 02:56
    PHP Autoload - 02:56
    PHP Namespaces Basics - 10:05
    PHP Namespace Autoload - 12:25
    What is Composer? - 16:50
    Composer Autoloading - 22:38
    Please support the channel by subscribing for more video on PHP.
    SUBSCRIBE: / @technotrax
    In this tutorial we'll learn the basic concept of PHP Autoload to load Classes automatically. We will also learn why we need to autoload PHP classes. We'll take a look at spl_autoload_register() method and its uses.
    We will learn to create separate PHP autoload files and how to read files from different directories to autoload the classes.
    We'll also dive into understanding Namespaces, its uses and importantly how they are used in autoloading. In the end, we also look in PHP Composer, how to setup Composer for your project and Composer autoloading. We will use practical examples to under PHP autoload and Namespace Autoloading in this tutorial.
    Composer Install: getcomposer.or...

Комментарии • 55

  • @MindExpanseChannel
    @MindExpanseChannel 4 года назад +17

    One of the best videos I have ever seen 👌

    • @TechnoTrax
      @TechnoTrax  4 года назад +1

      Hi Manzaa,
      Thank you for your kind words, really appreciate it.
      I hope I have earned your subscription.
      Thanks,
      TechnoTrax

  • @diederikslob
    @diederikslob 3 года назад

    Thanks, a very good explanation. I started to get a bit impatient halfway, but I am glad I watched everything after all.

  • @junjunghasudungan1905
    @junjunghasudungan1905 3 года назад

    thanks for create this video about autoloading and namespace, that's clearly and helpfully i think

  • @minhazulpr
    @minhazulpr Год назад

    Wow! It's awesome. Nice explanation. Thank Your for your effort.

  • @zakariyaechergui3265
    @zakariyaechergui3265 9 месяцев назад

    thank you sir for this beautiful video

  • @viggolagerstedtekholm4166
    @viggolagerstedtekholm4166 3 года назад

    Very good tutorial!

  • @NedumEze
    @NedumEze Год назад

    The best tutorial on this subject. Amazing delivery also. Thank you so much, Sir.
    May I ask please, if I have a folder for Classes, folder for Traits, another for Interfaces, how do I represent them in the psr-4 section of the composer.json file for autoloading?
    Can this load a functions.php file from a functions folder too?

  • @boshdev4616
    @boshdev4616 4 года назад +3

    Awesome Lesson! Can you do one regarding escaping conflicts between 3rd party loaded libraries using namespaces? Such as WordPress plugins when they have the same libraries which autoloaded in the theme directory.

  • @mehdi-vl5nn
    @mehdi-vl5nn 2 месяца назад

    old but gold

  • @kingsleyakindele5389
    @kingsleyakindele5389 3 года назад +1

    very great tutorial

  • @thatcoul
    @thatcoul 4 года назад +1

    Nice tutorial, I had to subscribe.

  • @NedumEze
    @NedumEze Год назад

    Dear Sir,
    Thank you very much for the clearest tutorial on this subject anywhere.
    I structured my directories same as yours, Namespaces, same as yours.
    I then 'Composer update', everything looked alright.
    But when I ran "Composer dump-autoload", I got errors:
    Saying, Class app\appClasses\Darabase located in c:/code/app\appClasses\New Database.php does not comply with psr-4 autoloading standards. Skipping. What is wrong?

  • @colorart4146
    @colorart4146 6 месяцев назад

    Oops! I should watch this video very beginning to learn php autoload! Thanks for your kind explaination in all the way.

  • @nhatnam4627
    @nhatnam4627 Год назад +1

    great tutorial

  • @adsensedd
    @adsensedd 3 года назад +1

    Excellent video, you really illustrate the problems with traditional autoloading first, nice one.

  • @resilientbit
    @resilientbit 27 дней назад

    Excellent! Clear and concise explaination for all aspects of autoloading in a single video. 👍

  • @thehunter11
    @thehunter11 3 месяца назад

    When you find just the perfect explanation... Thank you man !

  • @NedumEze
    @NedumEze 2 года назад

    It's a shame you didn't respond to my question concerning "uploading" files!
    Still, I do have another concern. Maybe you will consider helping. In the classmap segment, you did not add the vendor directory! The classmap.php file did not show anything in the Vendor directory, as cached.
    Is the Vendor directory automatically cached, elsewhere or must it be included in the composer.json classmap segment?

  • @yannisstaali3257
    @yannisstaali3257 3 года назад

    I hope you will continue making video !!!

  • @Rbog17
    @Rbog17 3 года назад +1

    Very solid and extremely simple to follow and understand. Well done and thank you.

  • @realchicagophill
    @realchicagophill 4 года назад +1

    Best

  • @Anth0ny85
    @Anth0ny85 2 года назад

    Thx alot... You really helped me.

  • @3vilJOHN
    @3vilJOHN 2 года назад

    Im happy to see this video on helping get organized. It helps avoid a backwards approach. What brought me here was my need for connecting a partials folder, having broke down my base html. Then the files started piling up, and I needed to get organized quick before I get too deep in the mud. I blindly tested organizing my files into folders thinking something gotta work. Then I found that include() is rendered useless if including from outside of the main directory. So I thought the approach spl had something to offer and learned the basics but this really answered my question towards modeling my organization. I just need to get my bootstrap to work in the subdirectories to stay organized. So I will do some playing around, something will give and somethings gotta work. Right now is the least amount of pressure in my studies learning code, mainly because I'm getting to freestyle variables after lots of practice, and it opens new doors in my mind, and really fades the struggles of learning. Rather than completely focused on class and learning to copy that; if finally get a chance to code some projects that are unique to me, and have more time to explore troubleshooting rather than worrying about passing grades in my studies, and all the hard work being able to read the code without blindness, just makes you feel worthy to explore the troubleshooting world without getting lost, and having abilities to understand the troubleshooting world. It's intriguing to me to find friends that are making these kinds of videos because are abilities really are questions that are a needle in a haystack. Thanks, I will post an update. About the solutions to my own answers I am searching for this help is leading me towards.

  • @omkarjere
    @omkarjere 4 года назад +2

    Absolutely brilliant!!! Thanks a lot for this.

    • @TechnoTrax
      @TechnoTrax  4 года назад

      You're very welcome! Hope I have earned your subscription :)

  • @youssef.elmoumen
    @youssef.elmoumen 2 года назад

    Clear and Easy to understand 💞

  • @Rbog17
    @Rbog17 3 года назад

    I loved the whole video. However, I got lost at the end when you got to the composer implementation. I understand how this behaves in a dev environment that has composer running, however, on a production remote server, I don't believe it's expected to have composer installed and running on there. So, in that scenario, how do the classes get autoloaded? Do we have to simply deploy the vendor directory? I thought vendor directory is only for local environment? Is there a build command that builds a deployable file for the autoload? I'm just confused about that part.

    • @rphlprnt
      @rphlprnt 3 года назад

      Composer should be on your production server. With that in mind, if you use a versioning system like git, your vendor directory should be ignored by git. Now I don't know how to have psr-4 in development and classMaps in production but I'm sure there's a way.

  • @arduinoguru7233
    @arduinoguru7233 2 года назад

    Thanks for this detailed tutorials, you are the only one that made me understand what name spaces means and why it's needed, it's very clever idea, and you simplified it a lot, Honesty more I watch a lot of RUclipsrs from India doing tutorials about PHP and other technical stuff, most of them were on hurry or skipping important stuff beginners need it, you are the only one explain everything step by step.

  • @NedumEze
    @NedumEze 2 года назад

    Wonderful handling of this great subject.
    Thank you very much Sir.
    But, may I ask: when you're uploading files to your hosting Server, would you upload also the Vendor folder and everything in it?

  • @mattynice7814
    @mattynice7814 2 года назад

    Absolutely excellent.

  • @sasha2209
    @sasha2209 2 года назад

    man thank you so much.

  • @asifurrahman5436
    @asifurrahman5436 2 года назад

    I've been searching this topic tutorials for 2 days and I just found yours.
    this is just most beautifully explained. I can't thank you enough ❤️

  • @MsAlokranjan
    @MsAlokranjan 3 года назад

    Sir please make 1 more video, how to export excel sheet from PhpSpreadsheet library in Codiginitor 4.1.1.

  • @juancarlosmunoz4699
    @juancarlosmunoz4699 3 года назад +1

    Incredibly useful.

  • @pavanobhan1105
    @pavanobhan1105 2 года назад

    Thank you, for those who have question on how to include test class, just give it namespace "test" and then, use it inside psr-4 as
    "test\\": "./"
    Here "./" means same directory as composer.json
    and if you want to include everything automatically, just use the below code:
    "autoload": {
    "classmap": [
    "./"
    ]

  • @ramrajkapar4936
    @ramrajkapar4936 3 года назад

    Important concept

  • @tiagovieira6653
    @tiagovieira6653 4 года назад +2

    Perfect dude.

  • @antonin2478
    @antonin2478 3 года назад +1

    Perfect tutorial about php namespace. subscribed 🔥

  • @yannisstaali3257
    @yannisstaali3257 3 года назад

    The best video about Autoloading and Namespace i've seen, so well explained thanks a lot !!!!

  • @memeta100
    @memeta100 4 года назад +1

    I subscribed bcz of the awesome content

    • @TechnoTrax
      @TechnoTrax  4 года назад

      Thank you so much! I am glad you liked the video.

  • @apna-ghar-883
    @apna-ghar-883 2 года назад

    Life was not easy without this.... Thnx man

  • @DaViDZeNhO
    @DaViDZeNhO 3 года назад

    Thanks man! Awesome explanation :)

  • @mugatu2017
    @mugatu2017 3 года назад

    very nice video my friend, thanks

  • @MrDeano2506
    @MrDeano2506 3 года назад

    Amazing tutorial but I have a problem when the namespace is nested I get failed to open stream: No such file or directory can anyone help please?

    • @rphlprnt
      @rphlprnt 3 года назад

      It probably means that your namespaces are not matching your directory structure. Please note that it is case sensitive.

  • @yorkshireplumbing
    @yorkshireplumbing 3 года назад

    Bloody awesome.

  • @photoclubio2577
    @photoclubio2577 Год назад +1

    Now i finally understand it. This is by far the clearest explanation video for the start with composer and namespaces! Thank you!

  • @NotMarkKnopfler
    @NotMarkKnopfler Год назад

    Logic in the spl_autoload_register method.... No thanks! Kind of explains why all these freaking php frameworks are so slow. I'll stick to explicitly including my files. Having the autoload method in a separate file is evil. It's hiding information away. Not a criticism of the video author - this is how it's done in PHP. I just don't agree with it. Appreciated the info on namespaces - I use them a lot in C++ but PHP's implementation seems more powerful and very elegant. 👍