...

View Full Version : accessing a function from inside a function inside a class



thesavior
03-20-2007, 02:24 AM
Okay, the title is confusing, but it is probobly something simple. Here is my class:


class tpl_parse
{
public $tplname; // What template to use
public $pagename; //What page to load
public $tpl; //Stores template info.
public $variable = array();

function run()
{
$tpl = file_get_contents(ROOT_PATH."themes/".$this->tplname."/templates/pages/".$this->pagename.".tpl");
preg_match("/{(.*)\:(.*)\:\}/", $tpl, $this->variable); // Search for variables
$this->variable[1]($this->variable[2]);
}
function file_include($tplfile)
{
$pos = strpos($file, "..");
if ($pos === false)
{
require(ROOT_PATH."templates/".$tplfile.".tpl");
}
else
{
echo "You cant do that!";
}
}

}

print_r of $this->variable is:



Array (
[0] => {file_include:header:}
[1] => file_include
[2] => header
)

The line: $this->variable[1]($this->variable[2]);

is giving me the error:


Fatal error: Call to undefined function file_include() in /mounted-storage/home45c/sub001/sc21473-GRUR/dev/forum/includes/functions/tpl_parse.php on line 35

As a note, tpl_parse is a class included and initialized in another class "superclass"

So to define $tplname i do: $superclass->tpl->tplname = "default";

Does anyone have any idea how I make the function in tpl_parse be used?

firepages
03-20-2007, 03:35 AM
consider what $this->variable[1]() actually translates to , it translates to functionname(); not $this->functionname();

2 options below.


<?
class tpl_parse{
function tpl_parse(){
$this->variable=array('file_include','blah');
}
function run(){//this will fail
$this->variable[0]($this->variable[1]);
}
function run1(){//looks like perl ;) but still works
$this->{$this->variable[0]}($this->variable[1]);
}
function run2(){//deference the array variable , works
$meth=$this->variable[0];
$this->$meth($this->variable[1]);
}
function file_include($tplfile) {
echo "trying to include {$tplfile}.tpl<br />";
}
}

$yaks=new tpl_parse();
$yaks->run1();
$yaks->run2();
$yaks->run();
?>


not sure why you are having to use regex ... but at this stage of the game there must be a better way !!

thesavior
03-20-2007, 05:44 AM
Okay, well that fixed that error and revealed a new one.

updated class file:



class tpl_parse
{
public $tplname; // What template to use
public $pagename; //What page to load
public $tpl; //Stores template info.
public $variable = array(); // Stores information about the variable in use

function run()
{
$this->tpl = file_get_contents(ROOT_PATH."themes/".$this->tplname."/templates/pages/".$this->pagename.".tpl"); // Load the tpl file into the variable
preg_match("/{(.*)\:(.*)\:\}/", $tpl, $this->variable); // Search for variables
// Call function $this->variable[1] with attribute $this->variable[2]
$func_name=$this->variable[1];
$this->$func_name($this->variable[2]);

}
function file_include($tplfile)
{
$pos = strpos($tplfile, ".."); // Make sure people dont go up directories
if ($pos === false) // Does this string exist?
{
// $this->tpl = str_replace($this->variable[0],file_get_contents(ROOT_PATH."themes/".$this->tplname."/templates/".$tplfile.".tpl"), $this->tpl); // Include another tpl
echo $this->tpl;
}
else
{
echo "You cant do that!"; // Yo! Boy! Get outta here!
}
}

}


and the error is:

Fatal error: Method name must be a string in /mounted-storage/home45c/sub001/sc21473-GRUR/dev/forum/includes/functions/tpl_parse.php on line 36

on line: $this->$func_name($this->variable[2]);

thesavior
03-20-2007, 06:23 AM
Well, I fixed it.

I needed to change:


preg_match("/{(.*)\:(.*)\:\}/", $tpl, $this->variable);

into


preg_match("/{(.*)\:(.*)\:\}/", $this->tpl, $this->variable);

however, I now am facing another issue.

It is now running the function for the first appearance of a {file_include:filename:} however, it doesn't continue. It should be sending every instance to the tpl parser. How do I run this loop?

aedrin
03-20-2007, 03:39 PM
preg_match_all()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum