Methods & Models Confusion

Well, unfortunately the project is private so i cant really share much but its basically a page method that rips through a structure field and turns each entry into a distinct area the page (banner, body text, image panel etc)

It also works out if the page has a banner background video or an image set, and after jumping through some hoops, if its an image, it sets the URL of the image url into a variable called $hero_bg.

Theres another method declared immediately under it to build the actual banner using bricks. I need to alter the structure, and for that i need the value of $hero_bg to use in the second method.

the first custom page method (in a plugin file) looks a bit like this (it’s huge)…

page::$methods['sectionBuild'] = function($page, $field='sections') {

	$content = '';

	foreach($page->{$field}()->toStructure() as $section):

		$classes = r($section->classes()->isNotEmpty(), str_replace(',',' ',$section->classes()->value()), false);
		$id = r($section->section_id()->isNotEmpty(), $section->section_id()->value(), false);
		$hero_bg = false;
		$video_hero = false;
		$modal = false;

		switch($section->type()->value()):
			case 'hero':
				$hero_results = $page->heroSectionBg();

				$text = $page->heroSection($section);
				// If image
				if($hero_results[1] == false):
					$hero_bg = $hero_results[0];
				// If video
				elseif($hero_results[1] == true):
					$video_hero = $hero_results[0];
					$text .= $hero_results[2];
					$hero_bg = $hero_results[3];
					$classes .= ' hero-video';
				endif;

				$classes = 'banner-innerpage-block blog-banner banner-pad'.r($classes, ' '.$classes);
				break;
			case 'top':
			default:
				$text = $page->textSection($section);
		endswitch;

                 $content.= r($modal, brick('div', $modal, ['id'=>$id]), brick('section', $video_hero.$text, ['class'=>$classes,'id'=>$id, 'style'=>$hero_bg]));

	endforeach;

	return $content;

};

Previously, the image was set as a background image via an inline style on the first method. For seo reasons, i need to make that an image tag inside the banner area instead, and for that i need the value $hero_bg available in the second method which looks like this:

page::$methods['heroSection'] = function($page, $section) {

$bannerimg = $hero_bg; // currently throws a woops
	
if(isset($page->heroParent) && $page->heroParent):
		$page = $page->parent();
		$section = $page;
	endif;

	if(!isset($page->headerField))
		$page->headerField = 'header';

	if(!isset($header))
		$page->header = $section->{$page->headerField}()->value();

	$text = brick('h1', $page->header);
	$text.= $page->breadcrumbs();
	$text = brick('div', $text, ['class'=>'inner-page-content']);

	$text = brick('div', brick('div', $text, ['class'=>'row']), ['class'=>'container-block']);

	$text.= brick('div', brick('img', false, ['src'=>url('assets/images/small-img/banner-logo.png')]), ['class'=>'banner-logo']);

	return $text;
};

How can get the value $hero_bg set in the first method in the second method as the value of $bannerimg variable?

The only way i can think is to just duplicate that case logic in the second method, but that seems a little sloppy. Must be a better way.