PHP equivalent of friend or internal
PHP doesn't support any friend-like declarations. It's possible to simulate this using the PHP5 __get and __set methods and inspecting a backtrace for only the allowed friend classes, although the code to do it is kind of clumsy.
There's some sample code and discussion on the topic on PHP's site:
class 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);
}
}
(Code proved by tsteiner at nerdclub dot net on bugs.php.net)
friend function in php?
You are most likely referring to class/variable scope. In php, you have:
- public
- private
- protected
But not friend
visibility. The protected
though is used when an object's members are to be made visible only to other extending/inheriting objects.
More Info:
- http://php.net/manual/en/language.oop5.visibility.php
PHP friend/package visibility
As stated here:
No. You can set a variable after declaring a namespace, but variables
will always exist in the global scope. They are never bound to
namespaces. You can deduce that from the absence of any name
resolution descriptions in
http://www.php.net/manual/en/language.namespaces.faq.php
Prevent invocation of instance method X from context != Y
To determine "what" called a particular method, look into debug_backtrace
I want to give access to protected properties to another class
I'm probably missing something, but it seems to me that all you want to do is extend the Form_Abstract class...
abstract class Form_Abstract {
protected $formAttributes = array('apple', 'orange');
protected $fields = array();
}
class My_Form extends Form_Abstract
{
function __construct() { }
function displayFormAttribute($num)
{
return $this->formAttributes[$num];
}
}
$x = new My_Form;
echo $x->displayFormAttribute(0); // apple
echo $x->displayFormAttribute(1); // orange
Extending gives you access to all of the public
and protected
variables and functions, however not the private
ones.
Friend System Posts Error
You should really rework how your database is structured. Since a friends list has the potential to be extremely big it needs its own table. But this is how you can get it to work using the current structure:
$friends = explode(',', $friends);
foreach($friends as $friend){
//do your query for each friend
}
PHP: friend classes and ungreedy caller function/class
You can call debug_backtrace(FALSE)
, which will then not populate the object index. This will speed it up a little bit, but generally, debug_backtrace is to be avoided in production code, unless your app is software tool where speed is not an issue or when using it for error handling.
From what I understand, you want to
- have an implicit reference to the caller available in the callee and
- outside access to private and protected properties to selected classes.
Both does not exist in PHP (and breaks encapsulation imho). For a discussion, please see
- [PHP-DEV] reference caller object and
- [PHP-DEV] Support for friend classes
How to set an Arrays internal pointer to a specific position? PHP/XML
If your array is always indexed consistently (eg. 'page1' is always at index '0'), it's fairly simple:
$List = array('page1', 'page2', 'page3', 'page4', 'page5');
$CurrentPage = 3; // 'page4'
while (key($List) !== $CurrentPage) next($List); // Advance until there's a match
I personally don't rely on automatic indexing because there's always a chance that the automatic index might change. You should consider explicitly defining the keys:
$List = array(
'1' => 'page1',
'2' => 'page2',
'3' => 'page3',
);
EDIT: If you want to test the values of the array (instead of the keys), use current()
:
while (current($List) !== $CurrentPage) next($List);
Related Topics
Execute a PHP Script from Another PHP Script
How to Wrap Long Lines Without Spaces in HTML
How to Check If Variable Is Array... or Something Array-Like
How to Detect Non-Ascii Characters in a String
How Many Days Until X-Y-Z Date
Should Lock_Ex on Both Read & Write Be Atomic
Forcing Access to _Php_Incomplete_Class Object Properties
How to Use Http_X_Forwarded_For Properly
How to Show Ajax Loading Gif Animation While the Page Is Loading
Codeigniter Csrf Valid for Only One Time Ajax Request
How to Use Two Submit Buttons, and Differentiate Between Which One Was Used to Submit the Form
Measure String Size in Bytes in PHP
What's the Difference Between Is_Null($Var) and ($Var === Null)
Convert to Date Format Dd/Mm/Yyyy
How to Extend a Class Dynamically
How to Use File_Get_Contents() to Compare Two Files