Using PHP Enable Internal Function

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

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

and what the function really does is.

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

The reason for this is that in the future I will distribute the directory, which will be view/archive/another_level/post.php . I don't want to go back everywhere in my code and change all the included paths.

This is currently what I have for my function, except that it seems that include is a call inside the function and not 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 include the file correctly?

EDIT:

There were no errors. Thanks to @Ramesh for the error checking code, ini_set('display_errors','On') , I was able to see that other “non-displayable” errors were detected in the included file, which seemed to cause the file to not be displayed ...

+6
source share
5 answers

This use case is explicitly documented :

If the inclusion occurs inside the function in the calling file, then all the code contained in the called file will behave as if it were defined inside this function. Thus, it will follow the variable scope of this function. An exception to this rule are magic constants that are calculated by the parser before being turned on.

IMHO, it’s easiest to maintain the base paths in constants (you already seem to do this to some extent), or even do a search and replace with a full site (which is a 30 second task in any decent editor) than overwriting all your included files to use global variables.

+7
source

Here is one way to solve the problem:

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

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

and your function will 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}"; } ?> 
+5
source

I think you should read about the scope of the variable .

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 will get its contents only in the context of this function.

+1
source

Until you actually indicate exactly what problem you have, I suspect that the variables are not available for your included file. A slightly awful way to partially solve this problem is to add this line before the include statement:

 extract($GLOBALS); 

This will import all the 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 want / expect. This is due to the fact that $GLOBALS only ever contains data from the global scope, it does not contain variables from the some_func() . There is no mechanism for accessing variables in the "parent" area in PHP.

It briefly follows that the approach you want to use (wrapping it in a function) will not work.

+1
source
 function view( $view, $file ) { switch ( $view ) { case 'archive' : $view = 'archive/temp'; break; case 'single' : $view = 'single'; break; } include( TEMPLATEPATH . "/view/".$view."/".$file ); } 

it works for me. You can also use include inside the case, or even better, embed the entire URL to be included inside the case each time.

0
source

Source: https://habr.com/ru/post/913017/


All Articles