PHP Design patterns in Codeigniter 4 - How they are used internally

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 มิ.ย. 2024
  • Lets see how Codeigniter 4 uses Design Patterns internally in its source code. A students asked me about how are Design Patterns used in real life, so I figured we can see source of some frameworks and applications. Let me know if you would like to see more of videos like this.
    Interested in learning real-life PHP design patterns?
    ➡️ www.ApplicableProgramming.com/
    If you are interested in programming courses related to web, please visit www.applicableprogramming.com/ to view courses where we show the problem, then we try to solve it the hard way, and then we show how we can benefit from proven solutions.
    ➤ LINKS
    ➡️ Source on github: github.com/codeigniter4/CodeI...
    ➡️ codeigniter.com/
    ➡️ Private "Applicable Programmers" facebook group / applicableprogrammers
    ➡️ Discord server / discord
    ➡️ Other tutorials: www.ApplicableProgramming.com
    Timestamps:
    Intro (00:00)
    Factory design pattern (01:00)
    Observer design pattern (06:11)
    Dependency Injection design pattern (13:40)
    Singleton design pattern (19:53)

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

  • @CharlyDS
    @CharlyDS 9 หลายเดือนก่อน +1

    Excellent video, CI4 has evolved ever since (4.4.0 as of today) and I've been catching up, since I spent years (about a decade!) using CI3. Also using WP and eventually would use Laravel, which I tried and is awesome but it's quite more opinionated, where as CI lets you code away very quickly.

    • @ApplicableProgramming
      @ApplicableProgramming  9 หลายเดือนก่อน +1

      Nice! I've used code igniter v2 when I "got inspired" by its active record as we were making something similar for our project at the time. That is when I looked at its source, and it is still somehow the cleanest source that I've see so far. It somehow still resonates with me. I haven't thought about Laravel as opinionated until now - although it is quite obvious now that you mentioned it... May I ask if you make your own projects for clients, what kind?

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

      @@ApplicableProgramming I was a Windows Desktop, Delphi programmer for many years (DOS Clipper before that). Always interested in Business apps. When the internet (intranet) came along I started developing ISAPI DLLs, then found PHP at version 4. And soon Codeigniter, when it was in version 3. Before that I tried building my own framework (great learning) so I know what you mean about looking at the sources. CI3 was really that clean - trivial to deploy and rock solid. Beyond some goodies, I'm migrating mostly because of the maintenance, the modern syntax (namespacing, saner loading) and thus easier inheritance in version 4. I make my own projects, yes, for administration or ECommerce solutions but also landing/customer sites. For most now I'm using WP; but custom solutions like a document manager or now, a treasury system are made in CI :) Laravel is *really* engaging but you must dedicate time to learn all the goodies they come up with everyday :D

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

    Thank you for your transparent guidance. I enjoy that you tackle these real world cases with PHP. I have created a few WooCommerce and Magento2 web stores, and at the moment I am thinking how I could create a lightweight framework which would be entirely dedicated for e-commerce solutions.
    I would want to strip whole app lifecycle of hundreds of database queries and strip all the unnecessary things away.
    Your content and examples has given me a lot of inspirations and ideas!
    If the framework realizes, I will tell you and share the repo for you :)
    This has been super valuable for me!
    May Jesus bless you in everything!

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

      Thank you for your kind words Antti, it is comments like this that make it all worth while :)
      What do you hope your framework will solve that other frameworks do not have?

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

      @@ApplicableProgramming Thank you for replying!
      I would want to achieve a small framework, which would fetch all necessary data from the database with very few separate queries.
      One of my WooCommerce shops executes more than 100 queries during one request, and those Magento2 shops even more.
      I would want the framework in its core to be just an API. I have thought it to be modular, for instance, the admin dashboard would be its own module.
      I would want it to contain basic SEO settings out of the box, and also to have a powerful cart.I would want to run the framework in a set of docker containets, where I could have an in-memory database for sessions and query cache.
      Email module would be much needed also.
      What do you think? Is there already something that contains this functionality?

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

      @@anttilappalainen9626 Sounds like a plan. I never heard of something similar, although I am not that up to date with all the frameworks. But it sounds like a quite specific tool, if there are enough people who need it and like the idea, it could get some traction.
      I would just keep one sentence as a "guide" for your project. something like "this framework is for ___ and solves this problem ___", and then you specialize it towards that problem.
      And from there, the devil is in the details

  • @anmarm.9487
    @anmarm.9487 ปีที่แล้ว +1

    Very informative... I like Codeigniter... especially version 4

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

      Thanks Anmar. Do you work with it professionally, or just for your own projects?

    • @anmarm.9487
      @anmarm.9487 ปีที่แล้ว

      @@ApplicableProgramming almost all my projects uses codeigniter4

    • @anmarm.9487
      @anmarm.9487 ปีที่แล้ว +2

      @@ApplicableProgramming almost all my projects uses codeigniter4 personal and professionally, as a matter of fact currently working on one project related to erp using ci4 experience is very positive with it. However, i find a lack of high end articles and tutorials on ci4 is bit frustrating. Tutorials such as structuring medium to large apps, class design as libraries, fat model or fat controllers (or when) or better still use slim model slim controller and fat library, most tutorials just copy from usergude.

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

      @@anmarm.9487 Oh tell me about it... That was my worst struggle with pretty much every framework I worked with.
      And it is not much better in other programming languages as well. But it do understand it is easier just to make a introduction youtube video, because creating something complex is both complex to code, and complex to explain easily.
      How long have you been working with codeigniter?

    • @anmarm.9487
      @anmarm.9487 ปีที่แล้ว +2

      @@ApplicableProgramming i been working with ci3 for couple of years and now im in love with ci4, no bloating, its just nice, however, userguide was much better explaining v3 but not the case in v4, so many features i get to discover with experiance. Also there is no good or best practice guide for ci4. I think it misses this part to excel again. But overall, in my humble opinion it beats laravel when it comes to simplicity and learning curve, laravel easy to start so difficult to see consistent coding style amongst laravel apps, however, thats not the case with ci4.

  • @Quynn-Oneal
    @Quynn-Oneal 2 ปีที่แล้ว +1

    5:50 Can't we use Factory pattern with Strategy pattern here to keep OCP?
    Great video thanks for creating such useful content.

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

      That is a great question. To be honest, I am not 100% sure myself where the border is between simple factory and the strategy. As far as I understand it, the Factory is working on creating new objects (creational pattern), and the Strategy is more concerned about specific action that needs to be performed (operational pattern). I guess that in this slide, it sort of depends on the rest of the application and what the problem it is trying to solve. Open-Closed Principle sort of applies to both approaches (or is probably broken with either of them), as you need somewhere to decide which class to use, and for that you either need to hardcode it somewhere in the code based on some conditions, or leave it up to the user to decide.
      I think of strategy more in terms of one particular method rather then the entire library. If different Shipping objects are different enough that they deserves their own object/library, then the factory is used on a bit higher level, deciding upon the correct object type. This is usually the case for external libraries (or their adapters), because they just work so differently that they just need their own objects created. Rarely will two shipping providers make libraries that look the same.
      However, if Shipping is so similar that only calculateShipping() method is different between all object types, then strategy can decide how the calculation is being done: calculateShipping($dhlCalculatorObj), or calculateShipping($upsCalcObj). From my experience, this is usually the case only for objects you fully control, like validateString($ip/$json/$email), or something like Log::setStrategy($file/$database/$memory/$anotherServer). Or if you need to calculate discount, you would have calculateDiscount($percentageStrategy/$fixedStrategy).
      But then the method you are referring inside the ShippingFactory, which is actually choosing the correct object, it could maybe be called for a Strategy. Or... What do you think? :)

    • @Quynn-Oneal
      @Quynn-Oneal 2 ปีที่แล้ว +1

      @@ApplicableProgramming Thanks for such detailed answer.
      My thought was to move that if else block to a strategy pattern and call a factory interface to retrieve the needed object. So IMO if else block is outside of factory to keep Single responsibility in place and have OCP with strategy. This way it may be easier to add new factory methods without editing the if else block.

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

      ​@@Quynn-Oneal that should work fine for this example.
      This has been bothering me for some time, curious thought how would you handle scenario where you want to say choose cheapest shipping method based on weight and different state? say:
      productWeight < 1kg and state is the same = UPS
      productWeight > 1kg and state is the same = dhl
      for any different state = Usps
      Where would you put this decision on which shipping handler to create (you need to call this logic multiple times when you are displaying the shipping, as the customer is adjusting the shopping cart)?
      Or if you are sending emails, and for @yahoo.com you would want to use Mailgun due to better spam resolving, and for everything else you would use MailSwift with native gmail account, because it is cheaper.
      Where would you decide which object should a factory create when you ask for $mailFactory->createMailerHandler($email) ? (or createShippingHandler($product))

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

    Please make videos on MediaWIki framework.

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

      You mentioned MediWiki, and I said I will make it next, I am sorry i had to skip it this time as I already planned for code igniter. I am now very low on time so i have to prioritize carefully. But i have it noted still :) so... One day suddenly

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

    I have watched a lot of your videos on design patterns and the SOLID principle... I have never heard you mentioning the Dependency INVERSION (not Injection) Principle, which is the "D" of the SOLID acronym... Is there a special reason for this, or are you one of them who thinks "D" stands for Dependency Injection Principle?
    Also, I have not seen any videos on "L", "I", either? Do you have them covered in your paid course?

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

      Nah, the same reason S is not that specifically covered :) That is because it didn't come up while making the projects :)
      it is not covered in my course, only some design patterns are and not other principles, although I could make a small course explaining those concepts now that you mention it.
      D is used a bit indirectly I think in the same lesson as O principle th-cam.com/video/kj3RxDjPfb8/w-d-xo.html and here in the code github.com/applicable-programming/darwin-cms/blob/master/src/Validation.php