Static Methods in Laravel/PHP: When and How?

  Рет қаралды 16,401

Laravel Daily

Laravel Daily

Күн бұрын

This is a shortened video version of a premium tutorial from Laravel Daily.
Full tutorial: laraveldaily.com/post/when-no...
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.com/courses
- Laravel QuickAdminPanel: quickadminpanel.com
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-archive.com/hom...
- My personal Twitter: / povilaskorop

Пікірлер: 35
@guccilive100
@guccilive100 10 ай бұрын
I don’t know if you are a kind of super hero for me, but each time when there’s something that blocks me or can’t understand… then booom!! here you are with a quick tips video about it. Can’t thank you enough… 🎉❤
@roberrt8386
@roberrt8386 11 ай бұрын
Static methods are great to initiate the class itself eg. ::fromValue
@fredvuni6240
@fredvuni6240 11 ай бұрын
I've been racking my brain over this concept since yesterday. Thanks!
@roman_zabigaliuk
@roman_zabigaliuk 11 ай бұрын
This is one of the topics where I feel immature. All my methods are static and I feel comfortable with it. And I uderstand that adult developers don't do that, and I need to step over myself once. Thak you for your reminder to me.
@matrixlander85
@matrixlander85 11 ай бұрын
Great video with multiple examples
@SussanRai
@SussanRai 11 ай бұрын
Static method is good when it doesn't depend on any dependency injection
@dominiks5318
@dominiks5318 10 ай бұрын
😅 I think author doesn't use Mockery and don't have the knowledge how to properly test the application.
@guccilive100
@guccilive100 10 ай бұрын
@@dominiks5318I tried mockery on a static method and was getting error that method was call 0 time but expected 1 and when I changed to be not called then the error the method was called 0 time but expected 1🤷🏿‍♂️
@AndyTalbot
@AndyTalbot 11 ай бұрын
Are they in any way better for performance? I tend to avoid them, and watching through the video it felt like you would generally too, but then you gave some very interesting examples at the end, and now I'm thinking of a few places in my projects where I could in theory use them.
@LaravelDaily
@LaravelDaily 11 ай бұрын
No, you wouldn't feel the performance difference
@johnnyw525
@johnnyw525 6 ай бұрын
@@LaravelDailyYou wouldn’t feel the difference, but why why instantiate a class when you don’t need to? 🤷‍♂️
@giacomogaravaglia6742
@giacomogaravaglia6742 11 ай бұрын
use NON static method (/classes) if you need to inject it via dependency injection.
@OliverKurmis
@OliverKurmis 7 ай бұрын
You always say "initialize" the class, but you mean "instanciate" the class. A class instance is an object, a data structure bound to the class methods. The object can be initialized with the help of the special "__construct()" method. Using static functions there is no object, but just a call of a function.
@johnnyw525
@johnnyw525 6 ай бұрын
It’s really basic: Use static when the method (function) doesn’t need to belong to an instance of the class. Ie. Does the class need to be instantiated for the function to operate? You can mix and match perfectly fine. There’s no need to instantiate a class if you don’t need to.
@guccilive100
@guccilive100 10 ай бұрын
Does anyone can point me to how to mock a static class?
@LaravelDaily
@LaravelDaily 10 ай бұрын
My tutorial specifically pointed out this reason of why NOT to use static classes. They are not mockable, maybe some tools are capable of it but I haven't done it
@ricohertha3537
@ricohertha3537 10 ай бұрын
@@LaravelDaily static methods
@free2idol1
@free2idol1 7 ай бұрын
If you want to mock static class, then it is better to first make that class as normal class (non-static), then make a facade class as a wrapper around that underlying class, then you can use mocking function provided by laravel to mock Facade.
@richardwheatley7194
@richardwheatley7194 11 ай бұрын
Why use them at all? What are the benefits?
@LaravelDaily
@LaravelDaily 11 ай бұрын
No need to initialize classes manually if you don't want to
@LeTrolli
@LeTrolli 11 ай бұрын
simple rule, do not use object just to call functions
@johnnyw525
@johnnyw525 6 ай бұрын
@@LaravelDailyYou say “initialise” but you mean “instantiate”: Create an object from the class. I’ve never heard “initialise” in this way before? Is it common?
@BlueJDev
@BlueJDev 11 ай бұрын
All that useful information and explanation, then there's me looking at the last example wondering why the functions have $value parameter but use $this-name and not $value in the function... Lol Not even a $value ?? $this-name
@zkabadu
@zkabadu 11 ай бұрын
Just don't mix and match static and non-static methods in service classes.
@johnnyw525
@johnnyw525 6 ай бұрын
You can mix and match.
@zkabadu
@zkabadu 6 ай бұрын
@@johnnyw525 yeah, you can, but you shouldn't.
@premce45
@premce45 5 ай бұрын
@@zkabadu Why ?
@zkabadu
@zkabadu 5 ай бұрын
@@premce45 I like to keep my classes consistent. Either I use the service class as a kind of utility class, then I would use static methods. Or I use dependency injection and then I will use non-static methods so I can access the injected dependencies. But I don't want to mix and match both responsibilities in one class.
@rs-nm7hp
@rs-nm7hp 11 ай бұрын
thanks for this video @povilas
@paulfontaine7819
@paulfontaine7819 11 ай бұрын
4:00 "that may work but it does not feel clean too me. I'm just personally not a big fan" . Please instead keep explaining when it does not work. Add you say some people feel they should never use static at all and thankfully you explain when it can be used.
@spicynoodle7419
@spicynoodle7419 11 ай бұрын
Chads use __call and __callStatic without having to care :D
@ward7576
@ward7576 11 ай бұрын
Chads don't care about this shit, stops arguing about it online and just makes the product and beats you to the market ;)
@spicynoodle7419
@spicynoodle7419 11 ай бұрын
@@ward7576 your product will die because you wrote all of it in a single function lol
@ward7576
@ward7576 10 ай бұрын
@@spicynoodle7419 I see I hurt you there. Get some fabric softener, you'll feel better.
Refactor "Senior" PHP Code with Early Returns
12:09
Laravel Daily
Рет қаралды 25 М.
Top 5 Laravel "Bad Practices" (My Opinion)
10:32
Laravel Daily
Рет қаралды 17 М.
КАРМАНЧИК 2 СЕЗОН 5 СЕРИЯ
27:21
Inter Production
Рет қаралды 605 М.
Super gymnastics 😍🫣
00:15
Lexa_Merin
Рет қаралды 14 МЛН
1 класс vs 11 класс  (игрушка)
00:30
БЕРТ
Рет қаралды 1,1 МЛН
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН
Exceptions in Laravel: Why/How to Use and Create Your Own
12:18
Laravel Daily
Рет қаралды 86 М.
Laravel solved race conditions
14:13
Aaron Francis
Рет қаралды 14 М.
Laravel: Avoid Try-Catch In Every Method (What To Do Instead)
4:45
Laravel Daily
Рет қаралды 27 М.
Laravel Code Review: Why NOT Use Repository Pattern?
14:21
Laravel Daily
Рет қаралды 75 М.
Static vs Non-Static Variables and Methods In Java - Full Simple Tutorial
11:29
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Рет қаралды 64 М.
DB Structure: 3 Mistakes Laravel Developers Make
3:17
Laravel Daily
Рет қаралды 10 М.
Livewire or Vue.js: Which to Use When?
12:13
Laravel Daily
Рет қаралды 12 М.
9 Tips for Shorter Laravel Code
10:16
Laravel Daily
Рет қаралды 60 М.
Ultimate Cookie Blizzard 🍦🍦🍦 54
0:10
Ice Cream Man
Рет қаралды 11 МЛН
Can you do it?! #kidsfun #kidslearning
0:17
J House jr.
Рет қаралды 37 МЛН
They RUINED Everything! 😢
0:31
Carter Sharer
Рет қаралды 15 МЛН
Backstage 🤫 tutorial #elsarca #tiktok
0:13
Elsa Arca
Рет қаралды 6 МЛН