私はクラスを持っているので、classAのメソッドをclassBのコンストラクタに渡し、後で実行するようにclassBのインスタンス変数に格納したいと思います。私はエラーを取得しているようにそれを実行しようとするとPassメソッド後で2番目のクラスに実行するための異なるクラス
class A
{
public function execute()
{
$ClassB = new ElgEmpAnalyticsImporterControllerImporterEmporioOrder(ConMW::getDB(), $this -> lPointFile, [$this, 'getLastPoints']);
$ClassB -> import();
}
public function getLastPoints(Array $keys)
{
$res = [];
forEach (json_decode(file_get_contents($this -> lastPointFile)) as $key => $value):
if (in_array($key, $keys)):
$res[$key] = $value;
else:
$res[$key] = '';
endif;
endforeach;
unset($key);
unset($value);
return $res;
}
}
classB
{
public $getLastPoints = null;
public function __construct(callable $getLastPoints)
{
$this -> getLastPoints = $getLastPoints;
}
public function import()
{
$lastPoints = $this -> getLastPoints(['idOrder', 'orderLastExport']);
}
}
「未定義のメソッドgetLastPointsの呼び出し()」
私は問題はClassBのインスタンス変数$ getLastPoints上の機能を格納上だと思います。私はこれを終えることができます。なぜなら私がコンストラクタで関数を実行すると、それは機能します。つまり、このようなクラスBのコンストラクタを変更すると、
classB
{
public $getLastPoints = null;
public function __construct(callable $getLastPoints)
{
$getLastPoints(['idOrder', 'orderLastExport']);
}
}
が動作します。
しかし、私は必要なのは、インポート関数内で外部関数を実行することです。
誰かお手伝いできますか?お時間を
おかげで、明確化のため
編集:私はこのようなコンストラクタ内の機能を実行することができますなぜ私の質問は次のとおりです。
$lastPoint(a,b)
が、私のようなインスタンス変数に呼び出し可能に割り当てますこれは
動作しません。
私は、phpが変数と機能に異なるストレージを使用していると読んでいます。 PHPはおそらくcallable $ lastPointsを変数として見ます。だから、呼び出し可能な$ lastPointsは、classBのインスタンスに動的関数として追加できますか?
Christoforos
これは本当の 'ElgEmpAnalyticsImporterControllerImporterEmporterOmder'ですか? – AbraCadaver
変数内のgetLastPoints()メソッドの値をクラスAに格納し、新しいBオブジェクトを作成するときにその変数を渡します。 –
残念です。AbraCadaver ElgEmpAnalyticsImporterControllerImporterEmporioOrderはClassBです – Christoforos