Jump to content

Including once


Xu Wei Jie

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.