0 レビュー
2 回答
php-クラスファイルをループに複数回含める方法は?
Class
ファイルを複数回インクルードしたいのですが、foreachループにファイルをインクルードしようとしましたが、Class<のために1回だけインクルードしました。 /code>。
以下は私のコードです。
1)ここにファイルを含めます。
$ordersinfo = $ordercon->get_orders_by_ids($p_order_id);
foreach ($ordersinfo as $key => $customerInfo) {
include("info_customer.php");
}
2) info_customer.php
class Demo
{
function __construct(argument)
{
echo "HEllo";
}
}
$test = new Demo;
oderinfo
に合計3つのレコードがありますが、{で宣言された
ファイル。Class
のため、ファイルは1回だけ含まれます。 -code-4}
どうすれば解決できますか?
わからない
0
レビュー
答え :
解決策:
クラスファイルを1回だけインクルードし、foreachループでクラスのインスタンスを作成します。次のようになります:
include("info_customer.php");
$ordersinfo = $ordercon->get_orders_by_ids($p_order_id);
foreach ($ordersinfo as $key => $customerInfo) {
$test = new Demo;
//Do stuff with $test
}
info_customer.php
:
class Demo
{
function __construct(argument)
{
echo "HEllo";
}
}
わからない
0
レビュー
答え :
解決策:
これを確認してください。
$array
を$ordersinfo = $ordercon->get_orders_by_ids($p_order_id);
Index.php
include( "info_customer.php");
//foreachをシミュレートするテスト配列
$array = array( '1'、 '2'、 '3'、 '4');
foreach($array as $ key => $ customerInfo){
$ test = new Demo($ customerInfo);
}
info_customer.php
コンストラクターargumentで、
argument
を$argument
に修正しました。
クラスデモ {{ function __construct($ argument) {{ エコー"ヘロ"; } } $test=新しいデモ;
ループ内でオブジェクト
new Demo($param)
を初期化する場合(上記の例のように)、ループ内でsetterとgetterを導入し、getterメソッドを呼び出す必要がある場合があります。各反復でオブジェクトを初期化するよりも。関数がたくさんあるクラスは、各反復を初期化するのにコストがかかります。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。