Php 确保调用了函数
我在做一个项目,我们在整个代码库中创建了许多对象 因此,对于某些对象,我们决定使用工厂来控制创建对象及其所有依赖项的过程。这是我们正在尝试做的一个例子:Php 确保调用了函数,php,design-patterns,yii2,software-design,Php,Design Patterns,Yii2,Software Design,我在做一个项目,我们在整个代码库中创建了许多对象 因此,对于某些对象,我们决定使用工厂来控制创建对象及其所有依赖项的过程。这是我们正在尝试做的一个例子: class CreateBranchFactory implements CreateBranchInterface { private $branch; public function __construct() { $this->branch = new Branch(); $thi
class CreateBranchFactory implements CreateBranchInterface {
private $branch;
public function __construct() {
$this->branch = new Branch();
$this->branch->scenario = 'create';
}
public function createBranch($branchForm) {
$this->branch->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->branch;
}
public function setOfficialNameAndPrinterName($offName, $printerName, $branchName) {
$this->branch->official_name = $offName ?? $branchName;
$this->branch->printer_name = $printerName ?? $branchName;
$this->branch->save();
}
public function createDependencies1() {
}
为了得到一份合适的合同,我为此创建了一个接口。此接口指定应定义的函数
interface CreateBranchInterface {
public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
public function createDependencies1();
}
但是,我的问题是,合同规定了应该定义的所有函数,但没有控制应该调用的函数。是否有任何设计模式,我可以使用,以确保这些功能得到调用 您不能通过使用接口来创建这样的契约-接口指定了可以调用哪些方法以及如何调用。调用所有这些方法是实现的一部分,接口无法提供。您需要创建带有实现的抽象类,并使用
final
关键字来禁止重写它:
abstract class CreateBranch {
abstract protected function getBranch(): Branch;
final public function createBranch($branchForm) {
$this->getBranch()->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->getBranch();
}
abstract public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
abstract public function createDependencies1();
abstract public function someOtherFunction();
}
您不能通过使用接口来创建这样的契约-接口指定了可以调用的方法和方式。调用所有这些方法是实现的一部分,接口无法提供。您需要创建带有实现的抽象类,并使用
final
关键字来禁止重写它:
abstract class CreateBranch {
abstract protected function getBranch(): Branch;
final public function createBranch($branchForm) {
$this->getBranch()->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->getBranch();
}
abstract public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
abstract public function createDependencies1();
abstract public function someOtherFunction();
}
为什么要确保调用所有函数?!您应该将必须完成的每项工作移动到一个函数中,并将操作的成功与所调用的函数联系起来。例如,该函数应该是构造函数,或者也将传递请求结果的函数。因为我正在做很多事情,所以我想对我正在做的事情有一个单独的“描述”。示例:当创建浏览器时,我1.createPostLaddress()2.CreateShippingAddress()3.CreateAPhoneLine(),…这只是一个示例,但问题是,我想要一个地方,在那里我可以看到我创建对象时需要做的所有事情,然后在某个地方创建一个封装函数,一次完成所有这些步骤。您希望能够单独调用每个函数,因为显然您希望能够独立地创建分支和打印机(例如)。但是,如果执行X的过程要求同时执行这两个操作,那么创建一个新的调用这两个方法的
函数doX
。为什么要确保调用所有函数?!您应该将必须完成的每项工作移动到一个函数中,并将操作的成功与所调用的函数联系起来。例如,该函数应该是构造函数,或者也将传递请求结果的函数。因为我正在做很多事情,所以我想对我正在做的事情有一个单独的“描述”。示例:当创建浏览器时,我1.createPostLaddress()2.CreateShippingAddress()3.CreateAPhoneLine(),…这只是一个示例,但问题是,我想要一个地方,在那里我可以看到我创建对象时需要做的所有事情,然后在某个地方创建一个封装函数,一次完成所有这些步骤。您希望能够单独调用每个函数,因为显然您希望能够独立地创建分支和打印机(例如)。但是,如果执行X的过程要求同时执行这两个操作,则创建一个新的函数doX
,该函数调用这两个方法。这是我第一次了解final关键字!!非常有帮助!谢谢:)这是我第一次知道最后一个关键词!!非常有帮助!谢谢:)