Я пытался использовать «$ this», но «php guy» в моей компании сказал мне, что я не могу использовать $ this, не ссылаясь на поле или функцию текущего класса (что он не может ссылаться на весь класс)
мой код выглядит следующим образом:
класс для рефакторинга:
class AccountBeforeRefactoring {
// (...)
public function makeTransfer($amount, $destinationAccount){
$transferFee = 1;
if ($amount > 1000){
$transferFee = 1 + $amount * 0.0001;
}
$this->connectToElixir();
// (...)
// (...)
// (...)
$this->debit($amount + $transferFee);
}
}
и он становится: класс извлечен - это был мой метод:
class TransferMaker {
private $account;
private $amount;
private $destinationAccount;
private $transferFee;
public function __construct($source, $amount, $destinationAccount){
$this->account = $source;
$this->amount = $amount;
$this->destinationAccount = $destinationAccount;
$this->transferFee = 1;
}
public function make(){
if ($this->amount > 1000){
$this->transferFee = 1 + $this->amount * 0.0001;
}
$this->account->connectToElixir();
// (...)
// (...)
// (...)
$this->account->debit($this->amount + $this->transferFee);
}
}
Правильно ли сделан конструктор?
и теперь мне нужно сделать объект MakeTransfer внутри моего класса Account - я пробовал таким образом - это нормально?
class Account {
// (...)
public function makeTransfer($amount, $destinationAccount){
new TransferMaker($this, $amount,
$destinationAccount).make();
}
}
и снова - могу ли я просто это "$ this" просто так?;) будет ли работать мой код?он действительно компилируется в Eclipse, но это может ничего не значить.