Friend function in php?

Does the php support function support, for example, how does C ++ support?

+5
source share
4 answers

You are most likely referring to the scope of the class / variable. In php you have:

  • the public
  • private
  • protected

But not friendvisibility. protectedalthough it is used when the members of an object should be visible only to other extending / inheriting objects.

Additional Information:

+7
source

No. You must declare it public.

+3
source

PHP . PHP5 __get __set , .

PHP:

HasFriends {   private $__ friends = array ('MyFriend', 'OtherFriend');

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);
}

}

+2

Friend - , , .

:

1. .

2. , .

3. (, ) . .

4. -, (.) .

5. It can be declared anywhere in the class without affecting its value. A member function of a class works with members of the object used to call it, while a friend function works with the object passed to it as an argument.

0
source

All Articles