2

I have two classes, A and B, that work together, like this:

  • Global scope instantiate Aobject.
  • Aobject instantiate Bobject as a public property.
  • Bobject contains Carray as a public property.

QUESTION:

How do I manipulate Carray from global scope, without copying it, AND without explicitly pointing at A->B->C for each operation?

PS: This is a minimal example. My chains could end up rather long, if no solution exists.

3
  • Create a getter function? Commented Jul 18, 2015 at 10:41
  • @u_mulder. A getter, containing what? ... I won't copy the array ... Commented Jul 18, 2015 at 10:44
  • If you need values of C from class A - create A::getC { return A->B->C; } If you need to set values of C from class A - create A::setC($key, $value) { A->B->C[$key] = $value; } Commented Jul 18, 2015 at 10:48

1 Answer 1

1

Use a reference. You will have to use A->B->C only once per scope.

$c = &$A->b->c;
$c['something'] = 'somethiing else';
Sign up to request clarification or add additional context in comments.

3 Comments

Can that technique pass a reference to A, inside B and global scope only needs to reference the reference in A ... as in each class can pass the reference forward in the chain? ... reference to reference to reference?
Objects are always passed by reference in PHP and you have to explicitly use 'clone' construction to copy them.
And yes, you can do that. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.