passing parameters to php include/require construct

前端 未结 4 592
隐瞒了意图╮
隐瞒了意图╮ 2021-02-12 11:01

I\'ve read quite a few posts that are very similar to the question I\'m about to ask, but I just wanted to be sure that there wasn\'t a more sophisticated way to do this. Any f

相关标签:
4条回答
  • 2021-02-12 11:05

    Include with parameters

    This is something I've used on my recent Wordpress project

    Make a function functions.php:

    function get_template_partial($name, $parameters) {
       // Path to templates
       $_dir = get_template_directory() . '/partials/';
       // Unless you like writing file extensions
       include( $_dir . $name . '.php' );
    } 
    

    Get parameters in cards-block.php:

    // $parameters is within the function scope
    $args = array(
        'post_type' => $parameters['query'],
        'posts_per_page' => 4
    );
    

    Call the template index.php:

    get_template_partial('cards-block', array(
        'query' => 'tf_events'
    )); 
    

    If you want a callback

    For example, the total count of posts that were displayed:

    Change functions.php to this:

    function get_template_partial($name, $parameters) {
       // Path to templates
       $_dir = get_template_directory() . '/partials/';
       // Unless you like writing file extensions
       include( $_dir . $name . '.php' );
       return $callback; 
    } 
    

    Change cards-block.php to this:

    // $parameters is within the function scope
    $args = array(
        'post_type' => $parameters['query'],
        'posts_per_page' => 4
    );
    $callback = array(
        'count' => 3 // Example
    );
    

    Change index.php to this:

    $cardsBlock = get_template_partial('cards-block', array(
        'query' => 'tf_events'
    )); 
    
    echo 'Count: ' . $cardsBlock['count'];
    
    0 讨论(0)
  • 2021-02-12 11:10

    There isn't a way to pass parameters to include or require.

    However the code that is included joins the program flow at the point where you include it, so it will inherit any variables that are in scope. So for example if you set $myflag=true immediately before the include, your included code will be able to check what $myflag is set to.

    That said, I wouldn't suggest using that technique. Far better for your include file to contain functions (or a class) rather than code that gets run straight off. If you've included a file containing functions then you can call your functions with whatever parameters you want at any point in your program. It's much more flexible, and generally a better programming technique.

    Hope that helps.

    0 讨论(0)
  • 2021-02-12 11:12

    You could have the required file return an anonymous function, and then call it immediately after.

    //required.php
    
    $test = function($param)
    {
        //do stuff
    }
    
    return $test
    
    //main.php
    $testing = require 'required.php';
    $testing($arg);
    
    0 讨论(0)
  • 2021-02-12 11:15

    In the past, many people have disagreed with this approach. But I think it is a matter of opinion.

    You can't pass _GET or _POST param via a require() or include() , but you can you first set a _SESSION key/value and pull it on the other side.

    0 讨论(0)
提交回复
热议问题