Dependency Injection and: What is the difference between calling a method and calling a method?

Source: Internet
Author: User
Laravel depends on _ construct (User $ user) in the constructor of the class, and then the subsequent methods are called directly using $ this-& amp; gt; user, what is the difference between using User: find () directly? Which one is recommended? Laravel depends on the injection _ construct (User $ user) in the constructor of the class, and then the subsequent methods are called directly using $ this-> user, and directly using User :: what is the difference between find? Which one is recommended?

Reply content:

Laravel depends on the injection _ construct (User $ user) in the constructor of the class, and then the subsequent methods are called directly using $ this-> user, and directly using User :: what is the difference between find? Which one is recommended?

This is the difference between static and dynamic php methods.
Static methods can be directly passed without class instantiation.class::functionBut must be included in the declaration.staticKeyword.
Dynamic methods can be called only through class instantiation.

$class= new Class;$class->somefunc();

The two methods are not superior or inferior. The key is the scenario used.
Generally, static methods are recommended if the logic is not closely related. Because dynamic methods actually need to implicitly pass in a parameter:$thisHas a slight impact on performance.

I don't understand php, but it is a basic idea in the programming field to "depend on interfaces rather than Implementation.
But the interface is not an actual object. how can we call it? this requires injection. we can physically concentrate all the injection into one place. In this way, you only need to modify the implementation of a class.

Class A{    function __construct(User $user){    }}Class B{    function __construct(){        $user = User::find(['id'=>1]);    }}$user = User::find(['id'=>1]);$a = new A($user);$b = new B();

Use
GoodUser extends User {}
Code needs to be changed

Class B{    function __construct(){        $user = GoodUser::find(['id'=>1]);    }}$user = GoodUser::find(['id'=>1]);$a = new A($user);$b = new B();

Have you found that class B has been changed .!!!
That is, violation of the open and closed principle.
This is generally the case ~ Xi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.