php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49060 require_last - for terminating code that must be executed after all includes.
Submitted: 2009-07-26 01:19 UTC Modified: 2011-01-01 16:18 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mccarre at uwindsor dot ca Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.10 OS: Mac OS X
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mccarre at uwindsor dot ca
New email:
PHP Version: OS:

 

 [2009-07-26 01:19 UTC] mccarre at uwindsor dot ca
Description:
------------
Because files are included at run time where the instruction is located, it may not ever be evaluated. This is to be expected.

However, this creates a problem with code that may call a function that is in one of the preceding included files. You generally want to call that function LAST, only after you've included all other files. (This is for nested include relations)

The only scenario I have found this relevant thus far is calling php functions using AJAX.

Reproduce code:
---------------
---
From manual page: function.include-once
---
I'll provide a simple example, because the one I came upon is too large.

Imagine you have fileA.php:
<?php
   include_once "fileB.php";
   include_once "fileC.php";
   include_once "callA.php";

   //when fileB.php includes callA.php and callA calls fncA and it tries to call fncC, although fncC is in this files includes list, that line has yet to be executed, therefor it will not be able to find the function. If you could include_last "callA.php", then the first include_last "callA.php" in fileB.php would be ignored, and the callA.php in fileA.php would be included after fileC.php is included. =)
   function fncA() { fncC(); }
?>

You normally wouldn't do the following, but if there was generic code to call an AJAX function here it would do this.
fileB.php:
<?php
   //reminder that include_once "fileC.php"; hasn't been executed yet.
   include_once "callA.php";
?>

fileC.php:
<?php
   fncC(){ print "hello"; }
?>

callA.php:
<?php
   fncA();
?>
I posted another example in the comments of include_once.

Expected result:
----------------
function not found.

Actual result:
--------------
with include_last, the function will be called and "hello" would be printed.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-26 01:23 UTC] mccarre at uwindsor dot ca
"Because files are included at run time where the instruction is located, it may not ever be evaluated."
Sorry I could have made this much more understandable by adding to it. Rephrasing I would say:
"Because files are included at run time where the instruction is located, it may not ever be evaluated; for example if a function is called that exits the script."

That should give a better idea of the complication I find with includes that causes my code road bump, which causes me to make this request.)
 [2009-07-26 01:37 UTC] mccarre at uwindsor dot ca
Hmmm on further through PHP could never achieve this with the way it works. It wouldn't make sense with an iterative scripting language.

The only possible generic function solution would require two functions. One to tell the function to ignore all other requests for the file, and then another to finally include the file. If you didn't have that second function you would have no idea when to include the file!

I saw a bug when I was posting this about symbolic includes. What I implied from that bug was an included file that didn't execute the code it had. If that was introduced, then people could use those for all files they didn't care about executing code with, then use include for code they wanted to be executed. That is the most elegant solution I can think of right now.

I suppose in that case my feature request would be to have an include that doesn't execute code, and is evaluated first before any other code.

(on further inspection that guys bug was about symbolically loading the functions of a file on include. According to everyone's comments PHP already does this. What I want is for that pesky loose code not to be executed if I tell it not to, by including it in a special way. This special way would could be called import perhaps.))
 [2011-01-01 16:18 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2011-01-01 16:18 UTC] jani@php.net
As requester himself figured out, not implementable.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Nov 20 17:00:01 2025 UTC