Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    accessing a function from inside a function inside a class

    Okay, the title is confusing, but it is probobly something simple. Here is my class:

    PHP Code:
    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?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    consider what $this->variable[1]() actually translates to , it translates to functionname(); not $this->functionname();

    2 options below.
    PHP Code:
    <?
    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();
    ?>
    [PS]not sure why you are having to use regex ... but at this stage of the game there must be a better way !![/PS]
    Last edited by firepages; 03-20-2007 at 03:38 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    Okay, well that fixed that error and revealed a new one.

    updated class file:

    PHP Code:
    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]);

  • #4
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    Well, I fixed it.

    I needed to change:
    Code:
    preg_match("/{(.*)\:(.*)\:\}/", $tpl, $this->variable);
    into
    Code:
    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?

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    preg_match_all()


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •