Autoload is changing Scope of variables included in autoloaded file











up vote
0
down vote

favorite












I'm having some trouble with my php project...



The autoload function:

spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});


When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.



someFile.php



require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}


the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.



Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:



'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'


I dont know if this is a problem with the code or with my enviorment.



I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)










share|improve this question
























  • Try require_once __DIR__ . '/../autoload.php';. You need to refer to the __DIR__ when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
    – Jeto
    Nov 19 at 19:20












  • Oh boy... Completly forgot about this... Thank you very much. :)
    – CKWDani
    Nov 19 at 19:25






  • 1




    The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
    – user9189147
    Nov 19 at 19:27










  • Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
    – CKWDani
    Nov 19 at 19:37















up vote
0
down vote

favorite












I'm having some trouble with my php project...



The autoload function:

spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});


When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.



someFile.php



require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}


the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.



Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:



'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'


I dont know if this is a problem with the code or with my enviorment.



I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)










share|improve this question
























  • Try require_once __DIR__ . '/../autoload.php';. You need to refer to the __DIR__ when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
    – Jeto
    Nov 19 at 19:20












  • Oh boy... Completly forgot about this... Thank you very much. :)
    – CKWDani
    Nov 19 at 19:25






  • 1




    The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
    – user9189147
    Nov 19 at 19:27










  • Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
    – CKWDani
    Nov 19 at 19:37













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm having some trouble with my php project...



The autoload function:

spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});


When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.



someFile.php



require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}


the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.



Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:



'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'


I dont know if this is a problem with the code or with my enviorment.



I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)










share|improve this question















I'm having some trouble with my php project...



The autoload function:

spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});


When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.



someFile.php



require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}


the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.



Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:



'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'


I dont know if this is a problem with the code or with my enviorment.



I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)







php scope include autoload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:28

























asked Nov 19 at 19:14









CKWDani

11




11












  • Try require_once __DIR__ . '/../autoload.php';. You need to refer to the __DIR__ when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
    – Jeto
    Nov 19 at 19:20












  • Oh boy... Completly forgot about this... Thank you very much. :)
    – CKWDani
    Nov 19 at 19:25






  • 1




    The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
    – user9189147
    Nov 19 at 19:27










  • Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
    – CKWDani
    Nov 19 at 19:37


















  • Try require_once __DIR__ . '/../autoload.php';. You need to refer to the __DIR__ when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
    – Jeto
    Nov 19 at 19:20












  • Oh boy... Completly forgot about this... Thank you very much. :)
    – CKWDani
    Nov 19 at 19:25






  • 1




    The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
    – user9189147
    Nov 19 at 19:27










  • Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
    – CKWDani
    Nov 19 at 19:37
















Try require_once __DIR__ . '/../autoload.php';. You need to refer to the __DIR__ when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
– Jeto
Nov 19 at 19:20






Try require_once __DIR__ . '/../autoload.php';. You need to refer to the __DIR__ when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
– Jeto
Nov 19 at 19:20














Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 at 19:25




Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 at 19:25




1




1




The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 at 19:27




The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 at 19:27












Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 at 19:37




Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 at 19:37

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381192%2fautoload-is-changing-scope-of-variables-included-in-autoloaded-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381192%2fautoload-is-changing-scope-of-variables-included-in-autoloaded-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin