回答編集履歴

2

編集

2019/04/14 08:08

投稿

kou0179
kou0179

スコア304

test CHANGED
@@ -13,30 +13,6 @@
13
13
  ```php
14
14
 
15
15
  <?php
16
-
17
- function hoge(){
18
-
19
- $names = ['tom','mic'];
20
-
21
- foreach ($names as $name) {
22
-
23
- function fuga_$name(){ // ←これではできない
24
-
25
- echo 'my name is'.$name.'.';
26
-
27
- }
28
-
29
- }
30
-
31
- }
32
-
33
-
34
-
35
- //「fuga_tom();」と「fuga_mic();」を実行したい
36
-
37
- hoge();
38
-
39
-
40
16
 
41
17
  class Human {
42
18
 

1

サンプルコード記載

2019/04/14 08:08

投稿

kou0179
kou0179

スコア304

test CHANGED
@@ -3,3 +3,81 @@
3
3
 
4
4
 
5
5
  [PHP: クラスの基礎 - Manual](https://www.php.net/manual/ja/language.oop5.basic.php)
6
+
7
+
8
+
9
+
10
+
11
+ ### サンプルコード
12
+
13
+ ```php
14
+
15
+ <?php
16
+
17
+ function hoge(){
18
+
19
+ $names = ['tom','mic'];
20
+
21
+ foreach ($names as $name) {
22
+
23
+ function fuga_$name(){ // ←これではできない
24
+
25
+ echo 'my name is'.$name.'.';
26
+
27
+ }
28
+
29
+ }
30
+
31
+ }
32
+
33
+
34
+
35
+ //「fuga_tom();」と「fuga_mic();」を実行したい
36
+
37
+ hoge();
38
+
39
+
40
+
41
+ class Human {
42
+
43
+ //宣言
44
+
45
+ private $name = '';
46
+
47
+
48
+
49
+ //コンストラクタ
50
+
51
+ function __construct($name) {
52
+
53
+ $this->name = $name;
54
+
55
+ }
56
+
57
+
58
+
59
+ // 名前の表示
60
+
61
+ public function print_name() {
62
+
63
+ echo 'my name is '.$this->name.'.';
64
+
65
+ }
66
+
67
+ }
68
+
69
+
70
+
71
+ $tom = new Human('tom');
72
+
73
+ $mic = new Human('mic');
74
+
75
+
76
+
77
+ $tom->print_name();
78
+
79
+ $mic->print_name();
80
+
81
+
82
+
83
+ ```