Search code examples
phpobjectget

Special multidimensional array


I have a difficult problem here (and I'm not sure that's possible).

I would like to call a class with the __get method:

$P = ClassName();
$P->A = 0.1;
$P->A->B = 0.5;

Which would return this:

$P->A should return 0.1 (and execute the __get method)

$P->A->B should return 0.5 (and execute the __get method on A)


Solution

  • To have

    $P->A
    

    return 1.5 is not a problem. You can do it with __get, though you do not have to. I do not believe, however you can do

    $P->A->B
    

    As "A" would be returning a number, not an object you would have nothing to call __get with. This, however would work:

    $P->A->getValue()
    $P->A->B
    

    But there is no way to get around it and make the code work as is.