11

Im trying to make a function that I can call as follows,

view( 'archive', 'post.php' );

and what the function really does is this.

include( 'view/archive/post.php' );

The reason for this is if in the future I expand the directory to be view/archive/another_level/post.php I dont want to have to go back everywhere in my code and change all the include paths.

Currently this is what i have for my function, except it appears that the include is being call inside the function, and not being called when the function is called...

function view( $view, $file )
    {
        switch ( $view )
        {
            case 'archive'  : $view = 'archive/temp';   break;
            case 'single'   : $view = 'single';     break;
        }

        include( TEMPLATEPATH . "/view/{$view}/{$file}" );
    }

How can I get this function to properly include the file?

EDIT:

There were no errors being displayed. Thanks to @Ramesh for the error checking code, ini_set('display_errors','On') I was able to see that there were other 'un-displayed' errors on the included file, which appeared to have caused the file not to show up...

4
  • 4
    there's nothing wrong with this - it should work as expected ... what problem exactly are you experiencing? Commented Apr 12, 2012 at 9:51
  • Im not getting an error, its just that nothing is printed... Commented Apr 12, 2012 at 9:58
  • 1
    Have you set error display On if not, place this code ini_set('display_errors','On'); and you will get error report Commented Apr 12, 2012 at 10:01
  • @Ramesh thanks for the code, i never knew about that. this helped me debug the problem. Commented Apr 12, 2012 at 10:18

5 Answers 5

12

That use case is explicitly documented:

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

IMHO, it's way simpler to keep base paths in constants (you already seem to be doing it to some extent) or even make a full-site search and replace (which is a 30 second task in any decent editor) than rewriting all your included files to use global variables.

Sign up to request clarification or add additional context in comments.

Comments

10

Here's one way you could solve the problem:

change the way you call your function so it looks like this:

include( view('archive','post') );

and your function would look like this:

<?php
function view( $view, $file )
{
    switch ( $view )
    {
        case 'archive': $view = 'archive/temp'; break;
        case 'single' : $view = 'single';       break;
    }

    return TEMPLATEPATH . "/view/{$view}/{$file}";
}
?>

Comments

3

While you don't actually state what the exact problem you are having is, I suspect that it is that variables are not available to your included file. A slightly horrible way to partially solve this problem is to add this line before your include statement:

extract($GLOBALS);

This will import all variables from the global scope into your function. However this will not make the function do exactly what you want. Consider this code:

function some_func () {
  $x = 2;
  view('archive', 'post.php');
}

$x = 1;

some_func();

In the included file, the value of $x will be 1, not 2 as you would want/expect. This is because $GLOBALS only ever contains data from the global scope, it does not contain the variables from the scope of some_func(). There is no mechanism for accessing the variables in the "parent" scope in PHP.

The long of the short of this is that the approach you want to use (wrapping it in a function) will not work.

Comments

2

I think you should read about the variable scope.

http://php.net/manual/en/language.variables.scope.php

The scope of a variable is the context within which it is defined.

So, if you include a file inside a function, you'll have its contents available only in the context of that function.

Comments

1
function view( $view, $file )
{
    switch ( $view )
    {
        case 'archive'  : $view = 'archive/temp';   break;
        case 'single'   : $view = 'single';     break;
    }

    include( TEMPLATEPATH . "/view/".$view."/".$file );
}

this works for me. Also you could use the include inside the case or even better built the whole url to include inside the case each time.

1 Comment

it works, but if you want to use the global scope from parent file, it is not a solution. But @sean9999 's Solution is quite simple and useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.