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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Variables not available in included files (included using a function)

    I have a function to check if files exist and if so include them

    PHP Code:
    public function tplFile($file)
        {
            foreach(
    $file as $file => $ext)
            {
                if(
    file_exists("template/$file.$ext"))
                {
                    include 
    'template/'.$file.'.'.$ext;
                }
            }        
        } 
    Its called like

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

    Thanks

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by tomharto View Post
    I have a function to check if files exist and if so include them

    PHP Code:
    public function tplFile($file)
        {
            foreach(
    $file as $file => $ext)
            {
                if(
    file_exists("template/$file.$ext"))
                {
                    include 
    'template/'.$file.'.'.$ext;
                }
            }        
        } 
    Its called like

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

    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; ?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Yeah i realize that , 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 ). I havent defined a new object no, the only time i created an instance of it is at the start of the index file.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by tomharto View Post
    Yeah i realize that , 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 ). 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.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Scope. You have included your file within a function. When complete and terminated you lose the variables declared within the include.

    Edit:
    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
    Last edited by Fou-Lu; 01-17-2012 at 08:13 PM.

  • #6
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    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 Code:
    <?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 Code:
    <?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 Code:
    <?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
    PHP Code:
    </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);
    ?>
    Last edited by tomharto; 01-17-2012 at 09:07 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Scope a copy of $controller to the tplFile() method. Although logically, that won't really fit with the pattern anymore.

  • #8
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Sorry, i dont quite get what you mean by 'Scope a copy of $controller'. Could you explain please?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    $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.

  • #10
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Okay, thank you for your help guys


  •  

    Posting Permissions

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