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.

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:


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.

i am searching this topic for many days as i have confused about this function i have cleared thanks a lot!! :slight_smile: