ä»ã«ãä½ã¨ã§ãããããã¯ããããã ãã©ãã¨ããããæãã¤ããæ¹æ³ã§å®è£ ãã¦ã¿ãã
ã¡ãªã¿ã«ããã§ããã±ã¼ã¸ãã©ã¤ãã¼ãã¨ããã®ã¯åãåå空éããã®ã¿ã¢ã¯ã»ã¹ã§ããã¨ããæå³ã§è¨ã£ã¦ããã
åèã«ããã®ã¯ä»¥ä¸ã®è¨äºã§ããã
<?php namespace { class PackagePrivateImplementor { private $nameSpaceName; public function __construct($nameSpaceName) { $this->nameSpaceName = $nameSpaceName; } function __call($name, $arguments) { if (!method_exists($this, '_' . $name)) { throw new \Exception('No such method: ' . $name); } $backTrace = debug_backtrace(); if (count($backTrace) < 3) { // main ããã®å¼ã³åºã throw new \Exception('Cannot invoke package private method from main!'); } else { $callerTrace = $backTrace[2]; } $name = '_' . $name; if (array_key_exists('class', $callerTrace)) { $caller = new \ReflectionClass($callerTrace['class']); } else if (array_key_exists('function', $callerTrace)) { $caller = new \ReflectionFunction($callerTrace['function']); } else { return; // ããã«å°éãããã¨ã¯ãªãã¯ã... } $callerNamespaceName = $caller->getNamespaceName(); if ($callerNamespaceName != $this->nameSpaceName) { // å¼ã³åºãå ãå¼ã³åºãå ã¨åãåå空éã®å ´åã®ã¿å¼ã¹ã throw new \Exception('Cannot invoke package private method from other package method!'); } $calleeMethod = new \ReflectionMethod($this, $name); $calleeMethod->setAccessible(true); $calleeMethod->invokeArgs($this, $arguments); $calleeMethod->setAccessible(false); } } } namespace foo\bar { class Loneliness extends \PackagePrivateImplementor { public function __construct() { parent::__construct(__NAMESPACE__); } private function _packagePrivateMethod($arg1, $arg2) { echo sprintf('This is package private method : arg1=%s, arg2=%s', $arg1, $arg2) . PHP_EOL; } } class Friend { static function invokePackagePrivateMethod($arg1, $arg2) { $loneliness = new Loneliness(); $loneliness->packagePrivateMethod($arg1, $arg2); } } function invokePackagePrivateMethod($arg1, $arg2) { $loneliness = new Loneliness(); $loneliness->packagePrivateMethod($arg1, $arg2); } } namespace foo\baz { class NotFriend { static function invokePackagePrivateMethod($arg1, $arg2) { $loneliness = new \foo\bar\Loneliness(); $loneliness->packagePrivateMethod($arg1, $arg2); } } function invokePackagePrivateMethod($arg1, $arg2) { $loneliness = new \foo\bar\Loneliness(); $loneliness->packagePrivateMethod($arg1, $arg2); } } namespace { // åãåå空éã®å¥ã®ã¡ã½ããããå¼ã¶ foo\bar\Friend::invokePackagePrivateMethod(11, 12); // åãåå空éã®å¥ã®é¢æ°ããå¼ã¶ foo\bar\invokePackagePrivateMethod(21, 22); // å¥ã®åå空éã®å¥ã®ã¡ã½ããããå¼ã¶ try { foo\baz\NotFriend::invokePackagePrivateMethod(31, 32); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; } // å¥ã®åå空éã®å¥ã®é¢æ°ããå¼ã¶ try { foo\baz\invokePackagePrivateMethod(41, 42); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; } // main ããå¼ã¶ try { $loneliness = new \foo\bar\Loneliness(); $loneliness->packagePrivateMethod(51, 52); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; } }
å®è¡çµæã¯ä»¥ä¸ã®ã¨ããã§ããã
This is package private method : arg1=11, arg2=12 This is package private method : arg1=21, arg2=22 Cannot invoke package private method from other package method! Cannot invoke package private method from other package method! Cannot invoke package private method from main!
åä½ã®èª¬æã¨èå¯ã¿ãããªãã®ã以ä¸ã«ã¾ã¨ããã
- ä»åã®ä¾ã§ã¯ã¢ã³ã¹ã³ã¤ãã®ã¡ã½ããåã§ããã±ã¼ã¸ãã©ã¤ãã¼ãã«ãããã¡ã½ãããå®ç¾©ããå¼ã³åºãããæã¯ã¢ã³ã¹ã³ãªãã®ã¡ã½ããåã§å¼ã¶ã¨ããã«ã¼ã«ã«ãã¦ããã
- ã¢ã³ã¹ã³ãªãã¡ã½ããã§å¼ã°ããæã« __call() ã§è§£æ±ºãããã
- ãã®ãããªå½åè¦åã使ç¨ããããªãå ´åã¯ã¢ããã¼ã·ã§ã³ã使ãã¨ããæ¹æ³ãããã¨æãã
- ãããã __call() ã§ããå¿ è¦ããªããå¼æ°ãé åã§åãåã __call() ã®ä»£æ¿ã¡ã½ãã (仮㫠verifyAndInvoke() ã¨ãã¦ã¿ã) ãç¨æãã¦ã以ä¸ã®ããã«ã¢ã³ã¹ã³ãªãã®ã¡ã½ãããå®ç¾©ããã°ããããã®å ´å㯠IDE ã§è£å®ãå¹ãåé¢ãããã±ã¼ã¸ãã©ã¤ãã¼ããªã¡ã½ããã®æ°ã ãã以ä¸ã®ãããªå®ç¾©ãå¿ è¦ã¨ãªãã
public function packagePrivateMethod($arg1, $arg2) { $this->verifyAndInvoke(func_get_args()); }
- debug_backtrace() ããã³ã¼ã«ã¹ã¿ãã¯ãåå¾ããã
- ã³ã¼ã«ã¹ã¿ãã¯ã¯ãã£ããè¨ãã¨ä»¥ä¸ã®ããã«ãªã£ã¦ãããã
æ·»å | ã³ã³ããã¹ã |
---|---|
0 | __call |
1 | packagePrivateMethod |
2 | å¼ã³åºãå |
-
- main ããã®å¼ã³åºãã§ããå ´åã¯ããã±ã¼ã¸å¤ã¨å¤æãããããããããããã³ã¼ã«ã¹ã¿ãã¯é·ã 3 以ä¸ãå¤å®ãã¦ããã(main ã®å ´åã¯å¼ã³åºãå ã®ã³ã¼ã«ã¹ã¿ãã¯ãåå¨ããªãã)
- ã¡ã½ãããé¢æ°ã«åå空éã®æ å ±ãããããããããåãåºããèªåã®åå空éã¨åããå¤å¥ããã
- åãã§ããå ´åã¯ãªãã¬ã¯ã·ã§ã³ã§ãããããã¦ããã¡ã½ãããå¼ã¶ã
- ãã®ä¾ã§ã¯ããã±ã¼ã¸ãã©ã¤ãã¼ãã®å¤å®ãåå空éã§ãã¦ãããããã¡ã¤ã«ãã¹ã§å¤æãããã¨ãã§ããã
- ã³ã¼ã«ã¹ã¿ãã¯ã«çµ¶å¯¾ãã¡ã¤ã«ãã¹ãå ¥ã£ã¦ãããããããã® dirname() ã§å¤å¥ããã°è¯ãã¯ãã§ããã