Asked  7 Months ago    Answers:  5   Viewed   32 times

Is there some equivalent of "friend" or "internal" in php? If not, is there any pattern to follow to achieve this behavior?

Edit: Sorry, but standard Php isn't what I'm looking for. I'm looking for something along the lines of what ringmaster did.

I have classes which are doing C-style system calls on the back end and the juggling has started to become cumbersome. I have functions in object A which take in object B as a parameter and have to call a method in object B passing in itself as an argument. The end user could call the method in B and the system would fall apart.

 Answers

30

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)

Wednesday, March 31, 2021
 
Crontab
answered 7 Months ago
74

PHP brings this:

$foobarobject->{"foomethod"}();

... and the coke and chips.

EDIT:

Although the term for the above is variable variables there is nothing specifically talking about doing it to an object in the manual. However, you can achieve the same thing with call_user_func:

call_user_func(array($foobarobject, "foomethod"));
Wednesday, March 31, 2021
 
hjalpmig
answered 7 Months ago
70

I think the correct syntax for referencing ranges of cells in another sheet is:

List!$A$1:$A$10

So you should try:

$objValidation->setFormula1('List!$A$1:$A$10'); // tested it, worked for me

Got the idea from http://phpexcel.codeplex.com/discussions/320393:

->setFormula1("Worksheet!A1:{$endCell}1");// work....

Although this guy had another problem with using named ranges.

Background: I think with:

$objValidation->setFormula1('"$List.$A$1:$A$10"');

you're explicity using the given string between the quotation marks as the list value as explained here: here (where you probably got this snippet in the first place) or here. But since you don't want to use fixed list items but dynamically referred ones, you should omit the double quotation marks.

Saturday, May 29, 2021
 
Ula
answered 5 Months ago
Ula
46

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
Saturday, May 29, 2021
 
NewPHP
answered 5 Months ago
93

PHP doesn't have nested functions, so in your example bar is essentially global. You can achieve what you want by using closures (=anonymous functions), which support binding as of PHP 5.4:

class A {
    function foo() {
        $bar = function($arg) {
            echo $this->baz, $arg;
        };
        $bar->bindTo($this);
        $bar("world !");
    }
    protected $baz = "Hello ";
}

$qux = new A;
$qux->foo();

UPD: however, bindTo($this) doesn't make much sense, because closures automatically inherit this from the context (again, in 5.4). So your example can be simply:

    function foo() {
        $bar = function($arg) {
            echo $this->baz, $arg;
        };
        $bar("world !");
    }

UPD2: for php 5.3- this seems to be only possible with an ugly hack like this:

class A {
    function foo() {
        $me = (object) get_object_vars($this);
        $bar = function($arg) use($me) {
            echo $me->baz, $arg;
        };
        $bar("world !");
    }
    protected $baz = "Hello ";
}

Here get_object_vars() is used to "publish" protected/private properties to make them accessible within the closure.

Saturday, May 29, 2021
 
dzm
answered 5 Months ago
dzm
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :