Last active
December 3, 2022 16:02
-
-
Save katsube/d74a13345cef80db43b9088bab0a2984 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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