

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP equivalent of friend or internal
<p>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 −</p><pre class="prettyprint notranslate">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); } }</pre>
- Related Questions & Answers
- What is the C# equivalent of C++ friend keyword?
- Difference Between Friend Function and Friend Class
- PHP – Get the internal settings of mbstring with mb_get_info()
- Embedded or internal Style Sheets in CSS
- Is there PHP basename() equivalent in MySQL?
- What is the PHP equivalent of MySQL's UNHEX()?
- The internal working of the ‘foreach’ loop in PHP
- Friend Pairing Problem
- Equivalent Circuit and Phasor Diagram of Synchronous Generator or Alternator
- Internal working of Python
- 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?
- Difference between internal rate of return and modified internal rate of return.
- Return the scalar dtype or NumPy equivalent of Python type of an object
Advertisements