回答編集履歴

2

修正

2020/07/03 14:13

投稿

m.ts10806
m.ts10806

スコア80850

test CHANGED
@@ -1,3 +1,23 @@
1
+ ## 大前提:エラーを読みましょう。
2
+
3
+
4
+
5
+ > Constant expression contains invalid operations
6
+
7
+ There must not be more than one property declared per statementphpcs
8
+
9
+ Google翻訳そのまま:定数式に無効な操作が含まれています
10
+
11
+ ステートメントごとに宣言されているプロパティは1つだけです
12
+
13
+
14
+
15
+ [エラーメッセージの読み方と対処, 検索や質問の原則](https://qiita.com/cannorin/items/eb062aae88bfe2ad6fe5)
16
+
17
+
18
+
19
+ -----
20
+
1
21
  単なるスコープの問題かと思います。
2
22
 
3
23
  $nowという変数をそこで定義しても、メソッド内はおろかクラスからも参照できません。

1

修正

2020/07/03 14:13

投稿

m.ts10806
m.ts10806

スコア80850

test CHANGED
@@ -12,27 +12,21 @@
12
12
 
13
13
  $a = "t";
14
14
 
15
-
16
-
17
15
  class test{
18
16
 
19
- protected $b = $a;
17
+ protected $b = $a; //PHP Fatal error: Constant expression contains invalid operations
20
18
 
21
19
  function c(){
22
20
 
23
- echo $b;
21
+ echo $this->b;
24
22
 
25
23
  }
26
24
 
27
25
  }
28
26
 
29
-
30
-
31
27
  $class = new test;
32
28
 
33
29
  $class->c();
34
-
35
-
36
30
 
37
31
  ```
38
32
 
@@ -44,27 +38,21 @@
44
38
 
45
39
  $a = "t";
46
40
 
47
-
48
-
49
41
  class test{
50
42
 
51
43
  //protected $b = $a;
52
44
 
53
45
  function c(){
54
46
 
55
- echo $a;
47
+ echo $a; //PHP Notice: Undefined variable: a
56
48
 
57
49
  }
58
50
 
59
51
  }
60
52
 
61
-
62
-
63
53
  $class = new test;
64
54
 
65
55
  $class->c();
66
-
67
-
68
56
 
69
57
  ```
70
58
 
@@ -77,3 +65,39 @@
77
65
  'created_at' => Carbon::now(),
78
66
 
79
67
  ```
68
+
69
+
70
+
71
+ 「何回も同じ記述書きたくない」ということでしたら、run()の中で$staff_logsも$now も定義するか、
72
+
73
+ $staff_logsを定義する別のメソッドを作ってrun()冒頭で呼び出して返却値を得るように書くことになるかと思います。
74
+
75
+
76
+
77
+ ---
78
+
79
+ ※ちなみに下記も同じ
80
+
81
+ ```php
82
+
83
+ <?php
84
+
85
+ class test{
86
+
87
+ protected $a = "a";
88
+
89
+ protected $b = $this->a; //PHP Fatal error: Constant expression contains invalid operations
90
+
91
+ function c(){
92
+
93
+ echo $this->a;
94
+
95
+ }
96
+
97
+ }
98
+
99
+ $class = new test;
100
+
101
+ $class->c();
102
+
103
+ ```