vars not available in extended class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

vars not available in extended class

Post by danwguy »

So I have a main class that sets and gets vars, then calls another class based on a variable that it sets, but in the extended class that it calls none of my $this-> vars are available unless I pass them through the constructor. Here's my code...

Code: Select all

<?php
include('scriptClass.php');
include('rebuttalsClass.php');
include('phrasesClass.php');
class SalesScreen {
public  $newClass;
public $campaign;
public $button;


	function __construct() {
	}
	
	function setUser($user) {
		$this->user = $user;
	}
	
	function setIndice($indice) {
		$this->indice = $indice;
	}
	
	function setPage($page) {
		$this->page = $page;
	}
	
	function setCampaign($campaign) {
		$this->campaign = $campaign;
	}
	
	function setState($state) {
		$this->state = $state;
	}
	
	function setCity($city) {
		$this->city = $city;
	}
	
	function setButton($button) {
		switch($button) {
			case 'intro':
				$this->button = '1';
				break;
			case 'display':
				$this->button = '2';
				break;
			case 'display':
				$this->button = '3';
				break;
			case 'verifier':
				$this->button = '4';
				break;
		}
	}
	
	function getPage() {
		return $this->page;
	}
	
	function getUser() {
		return $this->user;
	}
	
	function getIndice() {
		return $this->indice;
	}
	
	function getButton() {
		return $this->button;
	}
	
	function getCampaign() {
		return $this->campaign;
	}
	
	function getCity() {
		return $this->city;
	}
	
	function getState() {
		return $this->state;
	}
	
	/*function getObj() {
		if(!empty($this->arr['page']) && !empty($this->arr['button'])) {
			$this->view = $this->getContext($this-arr['page'], $this->arr['button'], $this->arr['user'], $this->arr['indice']);
		} elseif($this->arr['page'] == 'phrases') {
			$this->view = $this->getPhrases($this->arr['indice']);
		} elseif($this->arr['page'] == 'billing') {
			$this->view = $this->getBilling($this->arr['indice']);
		}
	}
		*/
	/*function buildView() {
		$this-arr = array('page' => $this->page, 'button' => $this->button, 'indice' => $this->indice, 'user' => $this->user);
	}
	*/
	function showIt() {
		$this->newClass = new $this->page($this->campaign, $this->button);
		$this->newClass->getContext();
		$this->newClass->getCallInfo();
		$this->display = $this->newClass->display();
		return $this->display;
	}
}
?>
and in one of the class it calls...

Code: Select all

<?php
class script extends SalesScreen {
public $script_page;
public $campaign;
public $page;
public $button;

	function __construct($campaign, $page) {
	$this->campaign = $campaign;
	$this->page = $page;
	}
	
	function getContext() {
			
			$query = "SELECT script_text FROM scripts WHERE campaign = '$this->campaign' AND button = '$this->page'";
			$run = mysql_query($query);
			while($row = mysql_fetch_assoc($run)) {
				$this->script_page = $row['script_text'];
			}
	}
	
	function getCallInfo() {
			/*$query = "SELECT display_name, cty, state, phrase, phrase_price, searches FROM leads WHERE indice = '$this->indice'";
			$run = mysql_query($query);
			while($row = mysql_fetch_assoc($run)) {*/
				$this->client['name'] = 'TEST CLIENT';
				$this->client['city'] = 'PORTLAND';
				$this->client['state'] = 'OREGON';
				$this->client['phrase'] = 'PORTLAND HOMES FOR SALE';
				$this->client['price'] = '79.00';
				$this->client['searches'] = '145';
				$this->client['engine'] = 'GOOGLE';
			
	}
	
	function globalReplace() {
		$replace_arr = array('{engine}', '{firstname}', '{agentname}');
		$replace_with = array($this->client['engine'], $this->client['name'], 'ROBERT');
		return str_replace($replace_arr, $replace_with, $this->script_page);
	}
	
	function display() {
		return $this->globalReplace();
	}
}
?>
as you can see I am passing $this->campaign in the constructor but I don't want to pass anything through that, I was under the assumption that the vars should be available in the extending class. I have tried many different ways like parent::getCampaign(); and parent::campaign but it always comes out blank. any ideas?
I guess I should show you the controller page that runs this just in case you need that so here it is...

Code: Select all

<?php
$conn = mysql_connect('localhost', 'root');
@mysql_select_db('script_builder');
include('../models/SalesScreen.php');

$page = $_GET['page'];
if(!empty($_GET['button'])) {
	$button = $_GET['button'];
	$extra = true;
} else {
	$extra = false;
}
$user = $_GET['user'];
$indice = $_GET['indice'];

$screen = new SalesScreen();
$screen->setPage($page);
if($extra) {
	$screen->setButton($button);
}
if(!empty($_GET['state'])) {
	$screen->setState($_GET['state']);
}
if(!empty($_GET['city'])) {
	$screen->setCity($_GET['city']);
}
if(!empty($_GET['campaign'])) {
	$campaign= $_GET['campaign'];
	$screen->setCampaign($campaign);
}

$screen->setUser($user);
$screen->setIndice($indice);


$display = $screen->showIt();

echo $display;

?>

Thank you in advance for any help
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: vars not available in extended class

Post by Christopher »

You have $campaign declared in both classes. The one in the script class is overloading the on in the SalesScreen class.
(#10850)
Post Reply