Friend function is best shared among a number of different classes. Such functions can be declared
either as member functions of one class or as global functions. In
either case they can be set to be friends of other classes, by using a
friend specifier in the class that is admitting them. Such functions can
use all attributes of the class which names them as a friend, as if
they were themselves members of that class.
A friend declaration is essentially a prototype for a member function, but instead of requiring an implementation with the name of that class attached by the double colon syntax, a global function or member function of another class provides the match.
For this PHP 5.3+ implementation you will need a base class. A class that should be extended by the classes that need to have friends. This base class provides the friend-architecture, so that it will need to be coded only once.
class MyBaseClass
{
protected static $friendClasses = array();
public function __get($name)
{
if (
// check if the caller's class is one of the friend classes
($trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)) &&
(isset($trace[1]['class']) && in_array($trace[1]['class'], static::$friendClasses))
) {
return $this->$name;
} else {
trigger_error('Member not available: ' . $name, E_USER_ERROR);
}
}
}
Then all you need to do do make add a friend to your class is declare it.
class Pooh extends MyBaseClass
{
protected static $friendClasses = array('Piglet', 'Tigger');
protected $feelings = 'sad';
}
class Piglet
{
public function beSensitive(MyBaseClass $Character)
{
echo 'You are so ' . $Character->feelings . '!';
}
}
Now a Piglet can access a Pooh's feelings.
$Pooh = new Pooh();
$Piglet = new Piglet();
$Piglet->beSensitive($Pooh);
Friend functions allow binary operators to be defined which combine private data in a pair of objects. This is particularly powerful when using the operator overloading features of C++. We will return to it when we look at overloading.
A friend declaration is essentially a prototype for a member function, but instead of requiring an implementation with the name of that class attached by the double colon syntax, a global function or member function of another class provides the match.
For this PHP 5.3+ implementation you will need a base class. A class that should be extended by the classes that need to have friends. This base class provides the friend-architecture, so that it will need to be coded only once.
class MyBaseClass
{
protected static $friendClasses = array();
public function __get($name)
{
if (
// check if the caller's class is one of the friend classes
($trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)) &&
(isset($trace[1]['class']) && in_array($trace[1]['class'], static::$friendClasses))
) {
return $this->$name;
} else {
trigger_error('Member not available: ' . $name, E_USER_ERROR);
}
}
}
Then all you need to do do make add a friend to your class is declare it.
class Pooh extends MyBaseClass
{
protected static $friendClasses = array('Piglet', 'Tigger');
protected $feelings = 'sad';
}
class Piglet
{
public function beSensitive(MyBaseClass $Character)
{
echo 'You are so ' . $Character->feelings . '!';
}
}
Now a Piglet can access a Pooh's feelings.
$Pooh = new Pooh();
$Piglet = new Piglet();
$Piglet->beSensitive($Pooh);
Friend functions allow binary operators to be defined which combine private data in a pair of objects. This is particularly powerful when using the operator overloading features of C++. We will return to it when we look at overloading.
Comments
Post a Comment