
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP equivalent of friend or internal
PHP doesn't support friend-like declarations. It can be simulated in PHP5 using the __get and __set methods and by inspecting a backtrace for the allowed friend classes. But this type of coding practice is considered to be clumsy −
class sample_friend { private $__friends = array('My_Friend', 'Other_Friend'); public function __get($key) { $trace = debug_backtrace(); if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) { return $this->$key; } // __get() code goes 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 goes here trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR); } }
- Related Articles
- What is the C# equivalent of C++ friend keyword?
- Microorganisms are friend or foe?
- The internal working of the ‘foreach’ loop in PHP
- PHP – Get the internal settings of mbstring with mb_get_info()
- Is there PHP basename() equivalent in MySQL?
- What is the PHP equivalent of MySQL's UNHEX()?
- PHP – Retrieve internal configuration variables of iconv extension using iconv_get_encoding() function
- Is their a JavaScript Equivalent to PHP explode()?
- What is the PHP stripos() equivalent in MySQL?
- Embedded or internal Style Sheets in CSS
- Difference Between Friend Function and Friend Class
- Madhu gave two fifth of a chocolate to her friend and one fourth or to another friend the quantity of chocolate left with her is
- Equivalent Circuit and Phasor Diagram of Synchronous Generator or Alternator
- Return the scalar dtype or NumPy equivalent of Python type of an object
- Friend Pairing Problem

Advertisements