<?php namespace Database; class Database { protected $adapter; public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; } } interface AdapterInterface {} class MysqlAdapter implements AdapterInterface {} 现在Database 类依赖于接口,相比依赖于具体实现有更多的优势。 假设你...
classXextendsY{ }// this is saying that "X" is going to complete the partial class "Y". ?> You would have your class implement a particular interface if you were distributing a class to be used by other people. The interface is an agreement to have a specific set of public methods ...
11class OrderShipmentController extends Controller 12{ 13 /** 14 * Ship the given order. 15 */ 16 public function store(Request $request): RedirectResponse 17 { 18 $order = Order::findOrFail($request->order_id); 19 20 // Ship the order... 21 22 Mail::to($request->user())->...
class Customer extends ActiveRecord { // ... public static function getDb() { // 使用 "db2" 组件 return \Yii::$app->db2; } } 查询数据(Querying Data) 定义Active Record 类后,你可以从相应的数据库表中查询数据。查询过程大致如下三个步骤:...
<?php class UserTest extends PHPUnit\Framework\TestCase { private $prophet; public function testPasswordHashing() { $hasher = $this->prophet->prophesize('App\Security\Hasher'); $user = new App\Entity\User($hasher->reveal()); $hasher->generateHash($user, 'qwerty')->willReturn('hashed_...
7class User extends Model 8{ 9 /** 10 * Get the phone record associated with the user. 11 */ 12 public function phone() 13 { 14 return $this->hasOne('App\Phone'); 15 } 16}The first argument passed to the hasOne method is the name of the related model. Once the relationship ...
The default rule class will not work here because it relies on statically declared patterns.We can create the following URL rule class to solve this problem.<?php namespace app\components; use yii\web\UrlRuleInterface; use yii\base\BaseObject; class CarUrlRule extends BaseObject implements Url...
For example, this change updates the return type for the Magento\Backend\Console\Command\AbstractCacheTypeManageCommand class from void to int, which extends Symfony\Component\Console\Command\Command and must return the int type. If you override or extend the Magento\Backend\Console\Command\Abstract...
classAContainerExtensionextendsSomeContainer{publicfunctionget($name,array$options=[]){}} These should be fine, right? Wrong. Starting in 7.1.0, the above emits anE_WARNINGdue to incompatible signatures. This is because PHP 7.1 addsnullable types, and considers the first signature equivalent to ...
As we have seen, Eloquent methods like all and get retrieve multiple records from the database. However, these methods don't return a plain PHP array. Instead, an instance of Illuminate\Database\Eloquent\Collection is returned.The Eloquent Collection class extends Laravel's base Illuminate\...