Hello,
I am looking for a way to stop variable scope leakage.

I have a class with a function to include a page something like this:

class functions{

function getView($viewTheme, $viewType, $viewTask, $viewFunction, $pageID=null, $itemID=null){
$baseView = ROOTME.DIR_VIEWS.$viewType.'/'.$viewTheme.'/'.BASE_VIEW_DIR.$viewTask.'/'.$viewFunction.'.php';

if(file_exists($baseView)){
include($baseView);	
}

}

}

My problem is, I am now wanting to reuse this function in some of the pages multiple times include, but the variables $viewTheme, $viewType, $viewTask, $viewFunction, $pageID, $itemID are needed again after the next call of the function, and they are overwritten by the next call of the function.

Is there a way to prevent this? For example make variables stay inside the function call and and only usable by the file it includes - and if that calls the function again, it again has its own variables?

Kind regards, and thanks in advance!

Recommended Answers

All 3 Replies

I'd make them properties. Then you can set them as you like, you can remove them from the function call, and they are still accessible after your first call.

Hi,
thanks for the response.

Would that be like making properties in the class? (e.g $this->viewTheme) ?
If so wouldn't they just be overwritten when the functions called again?

thanks

Only if you overwrite your current instance. If you need more, instantiate more classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.