Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Store DataObject in a variable so other functions can acces it?


Go to End


4 Posts   1720 Views

Avatar
micahsheets

Community Member, 165 Posts

22 April 2009 at 11:18am

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.

Avatar
bummzack

Community Member, 904 Posts

22 April 2009 at 8:17pm

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

Avatar
micahsheets

Community Member, 165 Posts

23 April 2009 at 7:53am

Edited: 23/04/2009 7:53am

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?

Avatar
bummzack

Community Member, 904 Posts

23 April 2009 at 10:56am

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