SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Require_once in function

    I have some php document that's quite large and I only want to require it when its needed.

    It can be required first in a function, and later owtside it. It can only be required 1 time, because it creates some variables on every require, and can only make one's per session.

    So I must use require_once, and if it's first required in the function, the variables that creates are not avalible outside the function, so = "Undefined variable", and if I use only require() = "Cannot redeclare ..." as I said.

    The required document is a third party and I can't change it. What I cant do to make like a Public require?

    PD: SORRY, I think the problem is not the function, is that the function is in an other document. I don't see a solution anyways, besides putting all together.

  2. #2
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow I'm gonna try to explain how I solve it but I'm confused about what I did XD

    First I created a new document. So we have Main.php "plane" php, function.php with the function, required.php the required third party with plenty of classes, functions..., and now newone.php the new document with a new function.

    Document Main.php requires function.php and newone.php

    newone.php have a function where I put in it all what Main.php and function.php needed to do with the required.php, and I return what they need, and as it is a function there are no problems to use it in any document.

    At this point I had the same problems than before but with more documents involved , but finally I come out with these in newone.php:

    PHP Code:
    function newfunction($var) {

          global 
    $variable_from_required_document;
          require_once(
    'required.php');
            
            
    // Do thing and return things

           

    As you can see I call that global variables and then require the document that makes them, but that’s the only way it works.
    I suppose that the second time the function is called it catches the variables from the first call, and it does not require required.php
    For last, if I put first the require_once and then the global, the first time is called that global overwrites the variables so it does not work, thats why is reversed.

    And that's it, I think I see it clear now.

  3. #3
    Non-Member Wisestepp's Avatar
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am searching this topic for many days as i have confused about this function i have cleared thanks a lot!!


Bookmarks

Posting Permissions

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