PHP - How to set the value of a multidimensional array property from "out" of the class?

advertisements

I'm trying to wrap my head around something that I can't seem to figure out a solution for. In class A I have a property which is a multidimensional array. In class B which is instantiated within class A, I'd like to set an index value for the property from class A. I run into a wall where I have no idea how to iterate through the property to set the value for the index I'd need to. Here's a test example of what I mean:

<?php

class SomeClass
{
    protected $class;
    protected $book;

    public function __construct()
    {
        $this->book["genre"]["title"] = "The Haunting";
    }

    public function SomeTest()
    {
        $this->class = new AnotherClass();
        $this->class->AnotherTest();
    }
}

class AnotherClass
{
    protected function setBook()
    {
        $indexes = func_get_args();
        $value   = array_pop($indexes);

        /**
         * So now I have the indexes which lead to the array
         * depth I'd like to set a value to, but where do I
         * go from here?
         *
         */
    }

    public function AnotherTest()
    {
        $this->setBook("something", "else", "1984");
    }
}

$someclass = new SomeClass();
$someclass->SomeTest();

?>

I'm so lost on how to do what I'm thinking of.


initiate class Another with a parameter, pass initiator class!

class SomeClass{
    public function SomeTest()    {
        $this->class = new AnotherClass($this);
        $this->class->AnotherTest();
    }
}

I use creator for these situations!

class AnotherClass{
    function __contruct($creator){
        $this->creator=$creator
    }

    function AnotherTest(){
        $this->creator->setBook("something", "else", "1984");
    }