Jump to:

23367 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Getting static variables from DataObjects or Controllers

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1535 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    Getting static variables from DataObjects or Controllers Link to this post

    Is there a way to get the value of a $protected variable on an object?

    In the case of ModelAdmin, it has a protected $currentRecord so when I am in the ModelAdmin in the CMS I expected to be able to:

    $record = Controller::curr()->currentRecord;

    However that does not work. If I go into CMS/ModelAdmin.php and add:

    function Record() {
    return $this->currentRecord;
    }

    and then do:

    $record = Controller::curr()->Record();

    that works but I don't want to modify a core file like that if I don't have to.

  • CrazyCoders
    Avatar
    Community Member
    32 Posts

    Re: Getting static variables from DataObjects or Controllers Link to this post

    Well you just said it yourself, it is protected, so no, there is no way that you can access it from outside the scope of the object itself, that is what it's used for...

    Private = Even inherited classes don't see it
    Protected = Only inherited classes can see that property
    Public = Anyone using this class can see the public members

    Anything else i can do for you?

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Getting static variables from DataObjects or Controllers Link to this post

    Isn't "Controller::curr()-> " asking the class itself? If that is so then I should be able to access the protected var since it is the current class?

  • CDGIDev
    Avatar
    Community Member
    9 Posts

    Re: Getting static variables from DataObjects or Controllers Link to this post

    No Controller::Curr() is calling something called a Static function/property. Static elements cannot be anything else than public or they would be useless. The method you defined before about modifying the source file is pretty much the only workaround if ->currentRecord is private. If ->currentRecord is protected, then you can define that property/function inside your own class such as :

    class MyModelAdmin extends ModelAdmin {

    function Record() {
    return $this->currentRecord;
    }

    }

    But note that this will not work if you are targeting a private property!

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Getting static variables from DataObjects or Controllers Link to this post

    It seems that even though the variable is protected using

    function Record() {
    return $this->currentRecord;
    }

    Doesn't work because $this isn't the controller.

    The only way I can get it to work is to modify core.

    1535 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.