Jump to content

Recommended Posts

Hi all,

 

How do I include once a library?

I mean not in the fashion of using include_once(). But implementing something that is similar to it

 

If the library is ever loaded, I would escape from loading it again.

 

Will this code work if I put in the beginning of the library code?

 

library.php
<?php
if (isset($pp_loaded__)) return 1;

$pp_loaded__ = true;

//load library functions here
?>

Link to comment
https://forums.phpfreaks.com/topic/149513-including-once/
Share on other sites

Actually i want to achieve something like when the user includes twice or more times the library itself, PHP won't prompt warnings like re declared functions and etc.

 

//test.php
<?php
include("library.php")
include("library.php")
include("library.php")
include("library.php")
?>

 

Using a list to keep track may be a good idea but if multiple files are involved, the list may be hard to pass from one file to another. It gets complicated.

 

Consider this example

 

//a.php
<?php 
include("library.php");
include("b.php");
?>

//b.php
<?php 
include("library.php");
?>

 

Thus I want to find a way to return out of the included library if it was ever loaded before.

Link to comment
https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785196
Share on other sites

I want to simulate the same behavior without using include_once. I wish to design the library such that the programmer need not be mindful whether to use include or include_once. He/She just need to include the library using whichever include he/she prefer and the library will detect for itself whether it has been loaded or not loaded before.

Link to comment
https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785204
Share on other sites

Anyway, I tried

 

if(isset($loaded)) return

else $loaded="set";

 

This code does not work though because $loaded seems to be a new instance every time the library is included.

 

Another way is to add another level of include_once inside the library =)

 

library.php

<?php

include_once("library_functions.php");

?>

Link to comment
https://forums.phpfreaks.com/topic/149513-including-once/#findComment-785572
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.