...

View Full Version : Variables not available in included files (included using a function)



tomharto
01-17-2012, 03:48 PM
I have a function to check if files exist and if so include them


public function tplFile($file)
{
foreach($file as $file => $ext)
{
if(file_exists("template/$file.$ext"))
{
include 'template/'.$file.'.'.$ext;
}
}
}

Its called like



$tplFiles = array('tpl.Header' => 'php',
'tpl.Nav' => 'php');

$controller->tplHeader($tplFiles);

I want to be able to use the $controller variable in the included files however i get the error
Notice: Undefined variable: controller

If i simply do

include ('theFile.php');
rather than the tplHeader function it works.

Is there anyway i can make it work using my function? Or is my method bad practice? If so id be happy to hear people ideas on how to make a better function for what im doing wrong :p

Thanks

BluePanther
01-17-2012, 03:54 PM
I have a function to check if files exist and if so include them


public function tplFile($file)
{
foreach($file as $file => $ext)
{
if(file_exists("template/$file.$ext"))
{
include 'template/'.$file.'.'.$ext;
}
}
}

Its called like



$tplFiles = array('tpl.Header' => 'php',
'tpl.Nav' => 'php');

$controller->tplHeader($tplFiles);

I want to be able to use the $controller variable in the included files however i get the error
Notice: Undefined variable: controller

If i simply do

include ('theFile.php');
rather than the tplHeader function it works.

Is there anyway i can make it work using my function? Or is my method bad practice? If so id be happy to hear people ideas on how to make a better function for what im doing wrong :p

Thanks

The function definition you've given, and how you call it, are two different functions. Do you know this?

The error means you haven't defined $controller. I take it the tplHeader and tplFile functions are methods of a class? Have you defined a new object of this class? $controller = new ClassName; ?

tomharto
01-17-2012, 03:59 PM
Yeah i realize that :p, tplHeader is a function in a controller which calls the tplFile function in a model (is that a bad method? im still new and learning MVC :p). I havent defined a new object no, the only time i created an instance of it is at the start of the index file.

BluePanther
01-17-2012, 04:14 PM
Yeah i realize that :p, tplHeader is a function in a controller which calls the tplFile function in a model (is that a bad method? im still new and learning MVC :p). I havent defined a new object no, the only time i created an instance of it is at the start of the index file.

Well, your error is saying there's no such variable $controller defined. That means, where or however you're using $controller->method, you haven't got an object instance assigned $controller (or anything for that matter). If you were wanting a static call to a method, you will have to use the class name itself, then the :: operator - ClassName::method

It would be helpful to see the full code concerned with this.

Fou-Lu
01-17-2012, 07:04 PM
Scope. You have included your file within a function. When complete and terminated you lose the variables declared within the include.


Wait, what's your whole error message? Is the undefined coming from a variable within one of these included files being declared OR is it being used? If its the former, you can't perform this in a function call due to scope, and if its the latter you need to declare it before its called within the include

tomharto
01-17-2012, 07:53 PM
EDIT: GEt errors showing.

Heres the full error
Notice: Undefined variable: controller in C:\Users\Administrator\Downloads\UniServer\www\MVC\template\tpl.Header.php on line 24

Fatal error: Call to a member function tplHeader() on a non-object in C:\Users\Administrator\Downloads\UniServer\www\MVC\template\tpl.Header.php on line 24


Here at the files (minus sensative info)

index.php

<?php

include_once("controller/Controller.php");
include_once('includes/config.php');


/* ======================================================

Loads the main controller used for pretty much everything

====================================================== */
$controller = new Controller();

/* ================================================================

Array of the template files (in order you want them to be included)

$tplFiles = array('fileName' => 'extension');

================================================================ */

$tplFiles = array('tpl.Header' => 'php',
'tpl.Nav' => 'php');

/* =================================

Includes the array of template files

================================= */

$controller->tplHeader($tplFiles);

/* =============================================

Loads the main file (if none defined uses index)

============================================= */

$controller->showPage();

/* ================================================================

Array of the template files (in order you want them to be included)

$tplFiles = array('fileName' => 'extension');

================================================================ */

$tplFiles = array('tpl.Footer' => 'php');

/* =================================

Includes the array of template files

================================= */

$controller->tplHeader($tplFiles);
?>




Controller.php

<?php

include_once("model/Model.php");
include_once("model/designModel.php");

class Controller
{
public $model;
public $dModel;
public $style;

public function __construct()
{
$this->model = new Model();
$this->dModel = new designModel();
}



public function print_rr($arr)
{
echo '<pre>';
print_r($arr);
echo '</pre>';
}

public function styleSheets($style)
{
$this->dModel->styleSheets($style);
}

public function javaSheets($Javas)
{
$this->dModel->javaSheets($Javas);
}





public function tplHeader($file)
{
$this->dModel->tplFile($file);
}




public function runSql($Sql)
{
$this->model->runSql($Sql);
}

public function showPage($page="index")
{
$this->model->includePage($page);
}
}

?>

designModel.php

<?php

class designModel extends Model {




public function styleSheets(array $style)
{
foreach($style as $file => $type)
{
if(file_exists("style/$file.css"))
{
echo "<link href=\"style/$file.css\" type=\"text/css\" media=\"$type\" rel=\"stylesheet\" />\r";
}
}
}

public function javaSheets(array $Javas)
{
foreach($Javas as $file => $type)
{
if(file_exists("js/$file.js"))
{
echo "<script src=\"js/$file.js\" type=\"$type\"></script>\r";
}
}
}

public function tplFile($file)
{
foreach($file as $file => $ext)
{
if(file_exists("template/$file.$ext"))
{
include 'template/'.$file.'.'.$ext;
}
}
}



}
?>

tpl.Header.php

</head>
<body>
<div id="header">
<h1>The Header File ('templates/tpl.Header.php')</h1>
</div>

<?php
/* ================================================================

Array of the template files (in order you want them to be included)

$tplFiles = array('fileName' => 'extension');

================================================================ */

$tplFiles = array('tpl.Nav' => 'php');

/* =================================

Includes the array of template files

================================= */

$controller->tplHeader($tplFiles);
?>

Fou-Lu
01-17-2012, 09:04 PM
Scope a copy of $controller to the tplFile() method. Although logically, that won't really fit with the pattern anymore.

tomharto
01-17-2012, 09:13 PM
Sorry, i dont quite get what you mean by 'Scope a copy of $controller'. Could you explain please?

Fou-Lu
01-17-2012, 09:41 PM
$var = 'a var';

function myfunc()
{
include 'mypage';
// $mypagevar will exist here.
}

myfunc();
// $mypagevar will not exist here.

// mypage
$mypagevar = 'my page var';
print $var;

Calling myfunc will produce no results. $var is not within the scope of myfunc, so therefore its not accessible from the scope of mypage. You need to pass an instance of $controller to the tplFile or provide an instance to the class which can be used in the method.

tomharto
01-17-2012, 09:48 PM
Okay, thank you for your help guys :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum