Jump to:

23478 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Store DataObject in a variable so other functions can acces it?

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: 1146 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    Store DataObject in a variable so other functions can acces it? Link to this post

    I have a function that gets a random DataObject of a particular page type. I need to be able to get that DataObject->ID in another function but I am not sure how to set a variable so that it can be set once and then accessed by other member functions.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Store DataObject in a variable so other functions can acces it? Link to this post

    Hi

    Not sure if I fully understand what you're trying to achieve. But what about something like that:

    class MyPage extends Page
    {
       protected $Data = null;
       
       public function getDataObj(){
          if($Data === null){
             // populate $Data with random data
          }
          
          return $Data;
       }
    }

    Using the getDataObj() method would return your $Data object. If it's not set (eg. null), the $Data variable will be initialized inside the getDataObj() method.
    If you want just a single "global" DataObject instance (as opposed to a DataObject for every instance of "MyPage"), you should probably make both the $Data variable and the method static. You would then access it using: MyPage::getDataObj();

    HTH

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Store DataObject in a variable so other functions can acces it? Link to this post

    Thanks for the explanation banal. I guess I need to study more on OOP. I don't have any understanding of what a static parameter is or what it does. I also don't know the difference between private and protected though I do know that protected parameters and methods can only be accessed by members of the class.

    Is the way these things work in SS the same as any php OOP approach? Could I use any php OOP tutorial to learn about the types of parameters and methods used?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Store DataObject in a variable so other functions can acces it? Link to this post

    Well. The concepts of OOP are more or less the same for all of the commonly used programming languages. PHP has some specialities like its magic-methods (http://www.php.net/manual/en/language.oop5.magic.php) but lacks for example strict typing or template classes/functions. Its probably a good idea to learn some basics about OOP and then also read the "Classes and Objects" Chapter in the PHP manual: http://www.php.net/manual/en/language.oop5.php

    To answer the static and private/protected question:
    Class members (be it functions or variables) declared as static, belong to a "class" whereas regular variables and functions belong to instances of a class. You could visualize a class as a "cookie cutter" and its instances are the cookies themselves. There's only one "cookie cutter", but there can be multiple instances (objects) of this class (cookies). Static members are commonly used to share data between all instances of a class, or to provide class methods without the need to instanciate classes.

    Private and protected members of a class aren't accessible from code outside of a class. The difference between private and protected is, that the former is only accessible within the class it was declared in, whereas the latter is accessible in subclasses. The follwing code illustrates this. SomePage has declared 2 variables, VarA and VarB. VarA is private and therefore only accessible inside the SomePage class. VarB is also accessible inside the AnotherPage Class, since it inherits from SomePage. From the outside, neither VarA nor VarB are accessible (in order for that to work, they need to be "public").

    class SomePage extends Page
    {
       private $VarA;
       protected $VarB;
    }

    class AnotherPage extends SomePage
    {
       public function doSomething(){
          echo $this->VarB; // works
          echo $this->VarA; // doesn't work
       }
    }

    // creating an instance of AnotherPage
    $a = new AnotherPage();
    echo $a->VarB; // doesn't work
    echo $a->VarA; // doesn't work

    1146 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.