php中的friend函数? PHP支持像C++支持的朋友函数吗?< /p>

php中的friend函数? PHP支持像C++支持的朋友函数吗?< /p>,php,Php,不。你必须声明它。 你最有可能是指类/变量范围。在php中,您有: 公开的 私人的 保护 但不是friendvisibility。当对象的成员仅对其他扩展/继承对象可见时,将使用受保护的 更多信息: PHP不支持任何类似于朋友的声明。可以使用PHP5 uuu get和uuu set方法来模拟这一点,并且只检查允许的友元类的回溯,尽管这样做的代码有点笨拙 PHP网站上有一些关于该主题的示例代码和讨论: 班上有朋友 { private$_friends=数组('myfriends','oth

不。你必须声明它。

你最有可能是指类/变量范围。在php中,您有:

  • 公开的
  • 私人的
  • 保护
但不是
friend
visibility。当对象的成员仅对其他扩展/继承对象可见时,将使用受保护的

更多信息:


    • PHP不支持任何类似于朋友的声明。可以使用PHP5 uuu get和uuu set方法来模拟这一点,并且只检查允许的友元类的回溯,尽管这样做的代码有点笨拙

      PHP网站上有一些关于该主题的示例代码和讨论:

      班上有朋友 { private$_friends=数组('myfriends','otherfriends')


      }[P/>看起来不:甚至维基百科似乎没有提到:没有找到朋友,但C++支持朋友类或函数\可能的副本

      public function __get($key)
      {
          $trace = debug_backtrace();
          if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {
              return $this->$key;
          }
      
          // normal __get() code here
      
          trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);
      }
      
      public function __set($key, $value)
      {
          $trace = debug_backtrace();
          if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {
              return $this->$key = $value;
          }
      
          // normal __set() code here
      
          trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);
      }