回答編集履歴

3

追記

2016/07/20 16:37

投稿

退会済みユーザー
test CHANGED
@@ -5,3 +5,99 @@
5
5
  [get_called_class()](http://php.net/manual/ja/function.get-called-class.php)
6
6
 
7
7
  [debug_backtrace()](http://php.net/manual/ja/function.debug-backtrace.php)
8
+
9
+
10
+
11
+ ---
12
+
13
+
14
+
15
+ ```php<?php
16
+
17
+
18
+
19
+ class Sample
20
+
21
+ {
22
+
23
+
24
+
25
+ private static $forbidenClass = [
26
+
27
+ 'Sample2'
28
+
29
+ ];
30
+
31
+
32
+
33
+ private static function init()
34
+
35
+ {
36
+
37
+ $trace = debug_backtrace();
38
+
39
+ foreach ($trace as $dbg) {
40
+
41
+ if ($dbg['class'] != __CLASS__ &&
42
+
43
+ in_array($dbg['class'], self::$forbidenClass)) {
44
+
45
+ $msg = sprintf('%s クラスから呼ぶことは禁止されています。', $dbg['class']);
46
+
47
+ throw new \Exception($msg);
48
+
49
+ }
50
+
51
+ }
52
+
53
+ }
54
+
55
+
56
+
57
+ public static function method()
58
+
59
+ {
60
+
61
+ self::init();
62
+
63
+ return true;
64
+
65
+ }
66
+
67
+
68
+
69
+ }
70
+
71
+
72
+
73
+ class Sample2
74
+
75
+ {
76
+
77
+
78
+
79
+ public static function method()
80
+
81
+ {
82
+
83
+ Sample::method();
84
+
85
+ }
86
+
87
+
88
+
89
+ }
90
+
91
+
92
+
93
+ try {
94
+
95
+ Sample2::method();
96
+
97
+ } catch (Exception $e) {
98
+
99
+ var_dump($e);
100
+
101
+ }
102
+
103
+ ```

2

追記

2016/07/20 16:37

投稿

退会済みユーザー
test CHANGED
@@ -3,3 +3,5 @@
3
3
 
4
4
 
5
5
  [get_called_class()](http://php.net/manual/ja/function.get-called-class.php)
6
+
7
+ [debug_backtrace()](http://php.net/manual/ja/function.debug-backtrace.php)

1

修正

2016/07/20 04:56

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,5 @@
1
1
  コンストラクタでフィルタリングすればいいのでは?
2
+
3
+
4
+
5
+ [get_called_class()](http://php.net/manual/ja/function.get-called-class.php)