1

I am struggling with extending a Plugin in OctoberCMS. I need to access a protected property, so I wanted to create a new getter, that would return the value, but it always returns NULL

Here an example code of the controller (I cannot extend it here, as this is a 3rd party plugin):

class Records extends Controller
{
    protected $some_value;
}

And this is how I implement it in the bootmethod of my Plugin:

Records::extend(function($controller) {
    $controller->addDynamicMethod('myValue', function() use ($controller) {
        return $controller->some_value;
    });
});

But this does not work. When I dump the $controller I get sth like this:

MyNamespace\MyPlugin\Controllers\Records {#1616 ▼
  #some_value: "1"
...

But when I want to return the value, it is null.

Asped
  • 3,083
  • 4
  • 31
  • 52
  • Not familiar with OctoberCMS but where are you setting `some_value` to something? As a test, what do you get (when you want to return a value) when you do this: `protected $some_value = 'Foo';` – Simon K Feb 01 '22 at 21:00
  • of course the value is being set somewhere. this is just an abstract of the controller ;) you see from the controller dump, that the value is set to `1`. So if I try your example, still I see `Foo` in the var_dump of the controller, but NULL in var_dump of `$controller->some_value` – Asped Feb 01 '22 at 21:15
  • I don't know OctoberCMS or how that "extend" method works, but have you tried: `return $this->some_value;`? – M. Eriksson Feb 01 '22 at 21:57
  • `extend` works with Closures, so the $controller variable should be the equivalent of `$this` in this context. If I would use only `$this` I would get the instance of my Plugin. I think this has to be a specific issue with OctoberCMS – Asped Feb 01 '22 at 22:27

1 Answers1

1

There is one hack that I used once. I was also facing the same issue

ref: https://tutorialmeta.com/octobercms/how-access-private-property-october-cms

// class
class Records extends Controller
{
    protected $some_value;
    // or private $some_value;
}

// in pugin
use Symfony\Component\VarDumper\Cloner\VarCloner;

Records::extend(function($controller) {
    $controller->addDynamicMethod('myValue', function() use ($controller) {
        $cloner = new VarCloner;
        $cloned = $cloner->cloneVar($controller);
        return $cloned->some_value;
    });
});

It should do the trick and you can access variables.

please comment if any doubt

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • interresting hack. I will try it. But I guessit uses Reflection.. so this way I could read values (which is enough in this case), but I guess I could not write – Asped Feb 02 '22 at 13:29
  • then it's not possible to access the protected variable directly as its meant to be protected :) – Hardik Satasiya Feb 02 '22 at 13:55
  • i get it is protected :) but this should be the idea of the Closure, to injecta blok of code,that has the ability to access the class from the "inside" – Asped Feb 03 '22 at 09:13
  • yes but seems PHP does not allow injected code to access the private variable. and yes I thought the same previously as if I extend and add public method I should be able to access protected but it is not the case .. it was not allowed me to access it. – Hardik Satasiya Feb 03 '22 at 09:24