Skip to content

Instantly share code, notes, and snippets.

@katsube
Last active December 3, 2022 16:02
Show Gist options
  • Save katsube/d74a13345cef80db43b9088bab0a2984 to your computer and use it in GitHub Desktop.
Save katsube/d74a13345cef80db43b9088bab0a2984 to your computer and use it in GitHub Desktop.
<?php
$shoppingCart = [1, 2, 2, 3];
// 合計金額を計算する
$total = 0;
// ★この部分を回答する★
// 合計金額を表示する
echo $total; // "800"と表示される
echo "\n";
/**
* Weaponクラス
*/
class Weapon {
private $item;
function __construct($id) {
// アイテム情報を取得
$this->item = $this->_findItem($id);
}
function getName(){
return( $this->item['name'] );
}
function getPrice(){
return( $this->item['price'] );
}
private function _findItem($id){
$table = [
['id'=>1, 'name'=>'ヒノキの棒', 'price'=>100],
['id'=>2, 'name'=>'ハガネの剣', 'price'=>200],
['id'=>3, 'name'=>'天空の剣', 'price'=>300]
];
// 一覧からidが一致するものを探す
for($i=0; $i<count($table); $i++){
if( $table[$i]['id'] === $id ){
return($table[$i]);
}
}
return(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment