企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### **Strong Typing & Water Fowl** ### In the previous chapters, we covered the basics of dependency injection: what it is; how it is accomplished; and several of benefits. The examples in the previous chapters also demonstrated the injection of *interface* into our *classes*, so before proceeding further, it will be beneficial to talk about interfaces in depth, as many PHP developers are not familiay with them. Before I was a PHP programmer, I was a .NET programmer. Do I love pain or what? In .NET, interfaces are everywhere. In fact, many interfaces are defined as part of the core .NET framework itself, and for good reason: many of the .NET languages, such as C# and VB.NET, are *strongly typed*. In general, you must define the *type* of object or primitive you will be passing into a method. For example, consider the following C# method: ~~~ <!-- lang: c# --> public int BillUser(User user) { this.biller.bill(user.GetId(), this.amount) } ~~~ Note that we were forced to define not only what type of arguments we will be passing to the method, but also what the method itself will be returning. C# is encouraging type *safety*. We will not be allowed to pass anything other than an ``User`` object into out ``BillUser`` method. However, PHP is generally a *duck typed* language. In a duck typed language, an object's avaliable methods determine the way it may be used, rather than its inheritance from a class or implementation of an interface. Let's look at an example: ~~~ <!-- lang:php --> public function billUser($user) { $this->biller->bill($user->getId(), $this->amount); } ~~~ In PHP, we did not have to tell the method what type of argument to expect. In fact, we can pass any type, so long as the object responds to the ``getId`` method. This is an example of duck typing. If the object looks like a duck, quacks like a duck, it must be a duck. Or, in this case, if the object looks like a user, and acts like a user, it must be one. But, does PHP have *any* strongly typed style features? Of course! PHP has a mixture of both strong and duck typed constructs. To illustrate this, let's re-write our ``billUser`` method: ~~~ <!-- lang:php --> public function billUser(User $user) { $this->biller->bill($user->getId(), $amount); } ~~~ After adding the ``User`` type-hint to our method signature, we can now gurantee that all objects passed to ``billUser`` either are a ``User`` instance, or extend the ``User`` class. There are advangates and disadvantages to both typing systems. In strongly typed languages, the compiler can often provide you with through compile-time error checking, which is extremely helpful. Method inputs and outputs are also much more explicit in a strongly typed language. As the same time, the explicit nature of strong typing can make it rigid. For example, dynamic methods such as ``whereEmailOrName`` that the Eloquent ORM offers would be impossible to implement in a strongly typed language like C#. Try to avoid heated discussions on which paradigm is better, and remember the advantages and disadvantages of each. It is not "wrong" to use the strong typing avaliable in PHP, nor is it wrong to use duck typing. What is wrong is exclusively using one or the other without considering the particular problem you are trying to solve.