1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Best way to have some functions in another PHP file

Discussion in 'XenForo Development Discussions' started by AndyB, Dec 9, 2013.

  1. AndyB

    AndyB Well-Known Member

    What is the best way to put some functions in another PHP file? For example I have an add-on and the Controller file works fine with some functions located near the end of the file. If I wanted to move these functions into another PHP file, how would I call this file? The other PHP file would be in the same directory as the Controller file.

    Thank you.
     
  2. AndyB

    AndyB Well-Known Member

    My goal is to make a file that is easier to edit by admins.
     
    Last edited: Dec 10, 2013
  3. Brogan

    Brogan XenForo Moderator Staff Member

    It's not really clear what you're trying to do but rather than editing php files, you should be creating the various options and fields in the ACP and then creating or extending the function as required.

    All editing/updating can then be done in the ACP without having to touch the files.
     
  4. AndyB

    AndyB Well-Known Member

    This is sound advice, Paul.

    Unfortunately some things are just too difficult to create options for. Editing a PHP file is not too difficult as long as the PHP file is fairly short and instructions are well documented.

    What I currently have is a ControllerPublic PHP file which has a function in it. I would like to move that function to its own file and have my ControllerPublic PHP file call this External.php file. If I understand correctly I need to create a class and call that class. Perhaps I also have to extend the the ControllerPublic PHP file?
     
  5. xf_phantom

    xf_phantom Well-Known Member

  6. AndyB

    AndyB Well-Known Member

    Thank you, xf_phantom. That was very helpful.
     
  7. AndyB

    AndyB Well-Known Member

    I was able to accomplish what I wanted. Here is an example.

    (temp1.php)

    PHP:
    <?php

    require_once('temp2.php');

    greet('Hello World');

    ?>
    (temp2.php)

    PHP:
    <?php

    function greet($name)
    {
        echo 
    $name;
    }

    ?>
    When I call temp1.php I get "Hello World" echoed.
     
  8. xf_phantom

    xf_phantom Well-Known Member

    hm, that's not what i was talking about:d

    I was talking about the configuration pattern because i thought that you need it
    Why would you want to use echo in xenforo?
    If you want to output something automatically IN your xenforo template, i would suggest to use the xen:callback or xen:helper feature;)
     
  9. AndyB

    AndyB Well-Known Member

    My example in post #7 is just an example of how a function can reside in another file.
     

Share This Page