dongyouzhui1969 2017-02-23 09:01
浏览 90
已采纳

PHP OOP中依赖注入,类型提示和组合的区别

I'm trying to learn the concepts of PHP OOP and I've watched a number of videos on the topic. In many of them they show an example like this :

class Person
{
    private $name;
    private $age;
    function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

}

class Business
{
    private $person;

    function __construct(Person $person)
    {
        $this->person = $person;
    }
}

So the problem is that one time they refer to this as Dependency Injection , other time they call it Type Hinting and third time they give this as Composition . So what exactly does this example represent ? Can you please explain the difference between them ?

  • 写回答

2条回答 默认 最新

  • du958642589 2017-02-23 09:48
    关注

    Dependancy injection is providing your application what it needs to function, which is any data. Most application are modular, they behave like separte components or entities. But all need to take in something to function.

    So, that thing they need is their dependancy.

    This can be passed via a class contructor, which is ideal as when the an object is initialized the contructor is the first function that gets called, so anything your app needs to work can be passed via the constructor. But sometimes you can pass the data directly to the method as an argument to your function/method Ex:

    # Generic Input validator
    class InputValidator{
        function isEmailValid($email){}
    }
    
    # Our main application 
    class UserRegistration(){
        function Register($inputValidator){
            $isEmailValid = $inputValidator->isEmailValid('foo@bar.com'); 
        }
    }
    
    # Instanciating the class and providing the dependancy
    (new UserRegistration)->Register(new InputValidator()); 
    

    In the above example, the UserRegistration->Register() depends on the class InputValidator() to register a user, we could have provided the email validator directly in the UserRegistration class, but we choose to pass it as a dependancy instead make our application as a whole S.O.L.I.D compliant.

    So, in short we are injecting the dependancy there. That is dependancy injection.

    Type Hinting is, much simpler to understand.

    Basically, if we extend our previous example and if you check Register(new InputValidator()); you can see that we passed it the class it needs to function, but someone mistakenly could also pass another class or even a string such as: Register('something'); which would break the application, since Method Register does not need a string. To prevent this, we can typehint it, in other words tell the Register function only to accept certain type of data : array, object, int ... or we can even explicitly inform it to take a class name by providing it before as

    $InputValidator = new InputValidator(); 
    Register(InputValidator $InputValidator);
    

    as for composition, this is a better read that I can provide What is composition as it relates to object oriented design?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c